r - Cumsum with plyr ddply is not keeping running total -
i'm trying use cumsum
, plyr
's ddply
have running total. however, don't understand why not work.
n = c(2, 3, 5) s = c("aa", "bb", "cc") dd = data.frame(n, s) # n s # 1 2 aa # 2 3 bb # 3 5 cc test <-ddply(dd, "n", transform, ttl = cumsum(n)) # n s ttl # 1 2 aa 2 # 2 3 bb 3 # 3 5 cc 5
what expect ttl
should is:
# 2 # 5 # 10
it'd great if explain i'm doing wrong here , how can obtain desired result. thanks.
as mentioned in comments, ddply works completly fine.
however think want ttl cumulative sum of n. use (also simpler) code:
dd$ttl <- cumsum(dd$n)
Comments
Post a Comment