【发布时间】:2015-02-03 04:59:20
【问题描述】:
我有一个数据框,其中包含有关一周中四天由不同人(由 id 列表示)完成的俯卧撑数量的数据。我必须执行以下操作
- 查找每个 id 的俯卧撑的运行总和(累积成本)
- 我想在每一天添加一列,显示第二天完成的俯卧撑数量。 (注意:由于在最后一天,我们不知道第二天做了多少俯卧撑,我们只考虑到第 n-1 行))
我首先按 (id,dayofweek) 对列进行“排列”,然后创建一个临时数据框,在该数据框上迭代地执行所有这些操作。这样做的问题是,在一个巨大的数据帧上,它非常非常慢。有没有更优雅的方式来做这两件事。请我的代码和下面的输入输出数据框
输入(排列后)
> df
id dayofweek pushupcount cumulativepushups nextdaypushupcount
1 1 day1 100 0 0
2 1 day2 240 0 0
3 1 day3 200 0 0
4 1 day4 170 0 0
5 2 day1 220 0 0
6 2 day2 190 0 0
7 2 day3 300 0 0
8 2 day4 150 0 0
9 3 day1 260 0 0
10 3 day2 160 0 0
11 3 day3 200 0 0
12 3 day4 210 0 0
输出
> df
id dayofweek pushupcount cumulativepushups nextdaypushupcount
1 1 day1 100 100 240
2 1 day2 240 340 200
3 1 day3 200 540 170
5 2 day1 220 220 190
6 2 day2 190 410 300
7 2 day3 300 710 150
9 3 day1 260 260 160
10 3 day2 160 420 200
11 3 day3 200 620 210
创建数据
#creating data
id = c(1,2,3,2,1,2,3,1,3,2,1,3)
dayofweek = c('day1','day2','day3','day1','day2','day3','day4','day4','day1','day4','day3','day2')
pushupcount = c(100,190,200,220,240,300,210,170,260,150,200,160)
df = data.frame(id,dayofweek,pushupcount,stringsAsFactors = FALSE)
代码
#arranding data in increasing order of day of week for each id
library('plyr')
df = arrange(df,id,dayofweek)
#adding the new columns
df$cumulativepushups = 0;
df$nextdaypushupcount = 0;
finaldf = NULL;
#the 'cumulativepushups' column is basically a running sum for each id
#the 'nextdaypushupcount' column is number of pushups for that id for the next day
(NOTE that since on the last day, we do not know how many pushups were done the next day, we consider only till rows n-1)
uniqueid = unique(df$id)
for(i in 1:length(uniqueid))
{
tempdf = df[which(df$id == uniqueid[i]),]
for(j in 1:(nrow(tempdf)-1))
{
if(j == 1)
{
tempdf[j,]$cumulativepushups = tempdf[j,]$pushupcount
}
else
{
tempdf[j,]$cumulativepushups = tempdf[j-1,]$cumulativepushups + tempdf[j,]$pushupcount
}
tempdf[j,]$nextdaypushupcount = tempdf[j+1,]$pushupcount
finaldf = rbind(finaldf,tempdf[j,])
}
}
df = finaldf
谢谢。
【问题讨论】: