可能这个问题已经在 SO 中得到解决和回答,但无法找到。我正在计算一个非常大的数据帧上的条件的累积和。看下面的例子
Data=data.frame("Catg"=c("A","A","A","A","A","B","B","B","C","C","C","D","D","D","D","D","D","D","D","E","E","F"),"Val"=c(67,42,12,32,28,1,11,9,38,61,75,99,22,44,89,99,51,34,82,99,74,42))
Res=NULL
UniqCatg=unique(Data$Catg)
for(i in 1:length(UniqCatg))
Res=c(Res, cumsum(Data[Data$Catg==UniqCatg[i],"Val"]))
Data$Res=Res
Data
没有for循环有没有聪明的方法?(如应用功能)