如何使用在前一行中引用其自身值的列计算在 R 中创建数据框?
How to create a dataframe in R with a column calculation that references its own value in the prior row?
我正在尝试使用 R 来计算作为库存函数的销售额作为销售额的函数。请参阅下面的数据快照。反正有计算这个的吗?
Group、Day和Build是自变量
销售额 = 滞后(销售额,1)* 构建
我得到了这个数据框:
Group <- c("A","A","A","A","A","B","B","B","B","B")
Day <- c(1,2,3,4,5,1,2,3,4,5)
Build <- c(1.5,2,.3,.5,.6,1.2,.9,1.2,1.2,.4)
Sales <- c(50000,NA,NA,NA,NA,20000,NA,NA,NA,NA)
正在尝试填充此数据框:
Group <- c("A","A","A","A","A","B","B","B","B","B")
Day <- c(1,2,3,4,5,1,2,3,4,5)
Build <- c(1.5,2,.3,.5,.6,1.2,.9,1.2,1.2,.4)
Sales <- c(50000,100000,30000,15000,9000,20000,18000,21600,25920,10368)
我们也可以使用 purrr
中的 accumulate
来做到这一点
library(dplyr)
library(purrr)
df1 %>%
group_by(Group) %>%
mutate(Sales = accumulate(Build[-1], ~ .y * .x, .init = first(Sales)))
# A tibble: 10 x 4
# Groups: Group [2]
# Group Day Build Sales
# <fct> <dbl> <dbl> <dbl>
# 1 A 1 1.5 50000
# 2 A 2 2 100000
# 3 A 3 0.3 30000
# 4 A 4 0.5 15000
# 5 A 5 0.6 9000
# 6 B 1 1.2 20000
# 7 B 2 0.9 18000
# 8 B 3 1.2 21600
# 9 B 4 1.2 25920
#10 B 5 0.4 10368
或将 base R
与 by
和 Reduce
一起使用
df1$Sales <- do.call(c, by(df1[3:4], df1$Group, FUN =
function(dat) Reduce(function(x, y) x * y,
dat$Build[-1], init = dat$Sales[1], accumulate = TRUE)))
数据
df1 <- structure(list(Group = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L), .Label = c("A", "B"), class = "factor"), Day = c(1,
2, 3, 4, 5, 1, 2, 3, 4, 5), Build = c(1.5, 2, 0.3, 0.5, 0.6,
1.2, 0.9, 1.2, 1.2, 0.4), Sales = c(50000, NA, NA, NA, NA, 20000,
NA, NA, NA, NA)), class = "data.frame", row.names = c(NA, -10L
))
我正在尝试使用 R 来计算作为库存函数的销售额作为销售额的函数。请参阅下面的数据快照。反正有计算这个的吗?
Group、Day和Build是自变量 销售额 = 滞后(销售额,1)* 构建
我得到了这个数据框:
Group <- c("A","A","A","A","A","B","B","B","B","B")
Day <- c(1,2,3,4,5,1,2,3,4,5)
Build <- c(1.5,2,.3,.5,.6,1.2,.9,1.2,1.2,.4)
Sales <- c(50000,NA,NA,NA,NA,20000,NA,NA,NA,NA)
正在尝试填充此数据框:
Group <- c("A","A","A","A","A","B","B","B","B","B")
Day <- c(1,2,3,4,5,1,2,3,4,5)
Build <- c(1.5,2,.3,.5,.6,1.2,.9,1.2,1.2,.4)
Sales <- c(50000,100000,30000,15000,9000,20000,18000,21600,25920,10368)
我们也可以使用 purrr
accumulate
来做到这一点
library(dplyr)
library(purrr)
df1 %>%
group_by(Group) %>%
mutate(Sales = accumulate(Build[-1], ~ .y * .x, .init = first(Sales)))
# A tibble: 10 x 4
# Groups: Group [2]
# Group Day Build Sales
# <fct> <dbl> <dbl> <dbl>
# 1 A 1 1.5 50000
# 2 A 2 2 100000
# 3 A 3 0.3 30000
# 4 A 4 0.5 15000
# 5 A 5 0.6 9000
# 6 B 1 1.2 20000
# 7 B 2 0.9 18000
# 8 B 3 1.2 21600
# 9 B 4 1.2 25920
#10 B 5 0.4 10368
或将 base R
与 by
和 Reduce
df1$Sales <- do.call(c, by(df1[3:4], df1$Group, FUN =
function(dat) Reduce(function(x, y) x * y,
dat$Build[-1], init = dat$Sales[1], accumulate = TRUE)))
数据
df1 <- structure(list(Group = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L), .Label = c("A", "B"), class = "factor"), Day = c(1,
2, 3, 4, 5, 1, 2, 3, 4, 5), Build = c(1.5, 2, 0.3, 0.5, 0.6,
1.2, 0.9, 1.2, 1.2, 0.4), Sales = c(50000, NA, NA, NA, NA, 20000,
NA, NA, NA, NA)), class = "data.frame", row.names = c(NA, -10L
))