R:ddply重复年度累积数据

时间:2012-05-10 14:34:01

标签: r plyr

与此问题here相关,但为了清楚起见,我决定提出另一个问题,因为“新”问题与原始问题没有直接关系。简而言之,我使用ddply累计总和三年中的每一年的值。我的代码从第一年获取数据,并在列的第二年和第三年行中重复。我的猜测是每个1年的大块被复制到整个专栏,但我不明白为什么。

Q值。如何在指定列的右行中获得每年的累计求和值?

[编辑:for循环 - 或类似的东西 - 很重要,因为最终我想根据列名列表自动计算新列,而不是手动计算每个新列。循环遍历列名列表。]

enter image description here

我经常使用ddply和cumsum组合,因此突然遇到问题会让人感到很烦恼。

[编辑:此代码已更新为我确定的解决方案,该解决方案基于@ Chase的答案]

require(lubridate)
require(plyr)
require(xts)
require(reshape)
require(reshape2)

set.seed(12345)
# create dummy time series data
monthsback <- 24
startdate <- as.Date(paste(year(now()),month(now()),"1",sep = "-")) - months(monthsback)
mydf <- data.frame(mydate = seq(as.Date(startdate), by = "month", length.out = monthsback),
                   myvalue1 = runif(monthsback, min = 600, max = 800),
                   myvalue2 = runif(monthsback, min = 1900, max = 2400),
                   myvalue3 = runif(monthsback, min = 50, max = 80),
                   myvalue4 = runif(monthsback, min = 200, max = 300))

mydf$year <- as.numeric(format(as.Date(mydf$mydate), format="%Y"))
mydf$month <- as.numeric(format(as.Date(mydf$mydate), format="%m"))

# Select columns to process
newcolnames <- c('myvalue1','myvalue4','myvalue2')

# melt n' cast
mydf.m <- mydf[,c('mydate','year',newcolnames)]
mydf.m <- melt(mydf.m, measure.vars = newcolnames)
mydf.m <- ddply(mydf.m, c("year", "variable"), transform, newcol = cumsum(value))
mydf.m <- dcast(mydate ~ variable, data = mydf.m, value.var = "newcol")
colnames(mydf.m) <- c('mydate',paste(newcolnames, "_cum", sep = ""))
mydf <- merge(mydf, mydf.m, by = 'mydate', all = FALSE)
mydf

1 个答案:

答案 0 :(得分:4)

我并没有真正关注你的for循环,但是你是否过于复杂?你不能直接使用transformddply吗?

#Make sure it's ordered properly
mydf <- mydf[order(mydf$year, mydf$month),]

#Use ddply to calculate the cumsum by year:
ddply(mydf, "year", transform, 
      cumsum1 = cumsum(myvalue1), 
      cumsum2 = cumsum(myvalue2))
#----------
       mydate myvalue1 myvalue2 year month   cumsum1   cumsum2
1  2010-05-01 744.1808 264.4543 2010     5  744.1808  264.4543
2  2010-06-01 775.1546 238.9828 2010     6 1519.3354  503.4371
3  2010-07-01 752.1965 269.8544 2010     7 2271.5319  773.2915
....
9  2011-01-01 745.5411 218.7712 2011     1  745.5411  218.7712
10 2011-02-01 797.9474 268.1834 2011     2 1543.4884  486.9546
11 2011-03-01 606.9071 237.0104 2011     3 2150.3955  723.9650
...
21 2012-01-01 690.7456 225.9681 2012     1  690.7456  225.9681
22 2012-02-01 665.3505 232.1225 2012     2 1356.0961  458.0906
23 2012-03-01 793.0831 206.0195 2012     3 2149.1792  664.1101

编辑 - 这是未经测试的,因为我在这台机器上没有R,但这就是我的想法:

require(reshape2)
mydf.m <- melt(mydf, measure.vars = newcolnames)
mydf.m <- ddply(mydf.m, c("year", "variable"), transform, newcol = cumsum(value))
dcast(mydate + year + month  ~ variable, data = mydf.m, value.var = "newcol")