使用dplyr的条件累积和

时间:2015-05-19 22:28:52

标签: r dplyr zoo

我的数据框看起来像这样,我想要两个单独的累积列,一个用于基金A,另一个用于基金B

free

但这完全不是我想要的,因为它向我展示了两个基金的运行总额,尽管只是在资金匹配的时候。 请帮助。

3 个答案:

答案 0 :(得分:9)

怎么样:

library(dplyr)

d %>% 
  group_by(Name) %>% 
  mutate(cA=cumsum(ifelse(!is.na(Fund) & Fund=="A",SalesAmount,0))) %>% 
  mutate(cB=cumsum(ifelse(!is.na(Fund) & Fund=="B",SalesAmount,0)))

输出:

Source: local data frame [10 x 8]
Groups: Name

   Name   Event SalesAmount Fund Cum.A.desired. Cum.B.desired.   cA   cB
1  John Webinar          NA   NA             NA             NA    0    0
2  John    Sale        1000    A           1000             NA 1000    0
3  John    Sale        2000    B           1000           2000 1000 2000
4  John    Sale        3000    A           4000           2000 4000 2000
5  John   Email          NA   NA           4000           2000 4000 2000
6   Tom Webinar          NA   NA             NA             NA    0    0
7   Tom    Sale        1000    A           1000             NA 1000    0
8   Tom    Sale        2000    B           1000           2000 1000 2000
9   Tom    Sale        3000    A           4000           2000 4000 2000
10  Tom   Email          NA   NA           4000           2000 4000 2000

如果需要,之后可以用NA替换结果列中的零:

result$cA[result$cA==0] <- NA
result$cB[result$cB==0] <- NA

您的输入数据集:

d <- structure(list(Name = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L,     2L, 2L, 2L), .Label = c("John", "Tom"), class = "factor"), Event = structure(c(3L,     2L, 2L, 2L, 1L, 3L, 2L, 2L, 2L, 1L), .Label = c("Email", "Sale",     "Webinar"), class = "factor"), SalesAmount = c(NA, 1000L, 2000L,     3000L, NA, NA, 1000L, 2000L, 3000L, NA), Fund = structure(c(NA,     1L, 2L, 1L, NA, NA, 1L, 2L, 1L, NA), .Label = c("A", "B"), class = "factor"),         Cum.A.desired. = c(NA, 1000L, 1000L, 4000L, 4000L, NA, 1000L,         1000L, 4000L, 4000L), Cum.B.desired. = c(NA, NA, 2000L, 2000L,         2000L, NA, NA, 2000L, 2000L, 2000L)), .Names = c("Name",     "Event", "SalesAmount", "Fund", "Cum.A.desired.", "Cum.B.desired."    ), class = "data.frame", row.names = c(NA, -10L))

答案 1 :(得分:3)

这是一种使用zoodata.table来推广更多资金的方法:

# prep
require(data.table)
require(zoo)
setDT(d)
d[,Fund:=as.character(Fund)]         # because factors are the worst
uf  <- unique(d[Event=="Sale"]$Fund) # collect set of funds

首先,在相关的观察子集中分配累计销售额:

for (f in uf) d[(Event=="Sale"&Fund==f),paste0('c',f):=cumsum(SalesAmount),by=Name]

然后,进行最后一次观察:

d[,paste0('c',uf):=lapply(.SD,na.locf,na.rm=FALSE),.SDcols=paste0('c',uf),by=Name]

答案 2 :(得分:2)

您可以将@ Marat的答案略微缩短为一个targetSdkVersion来缩短@ Marat的答案:

mutate