根据条件基于行值拆分数据

时间:2013-08-30 10:06:52

标签: r split plyr subset

这就是我的数据:

df <- structure(list(`1` = c(1 , 2 , 3 , 4 , 5  ,6 , 7 , 8 , 9, 10 ,11 ,12, 13, 14 ,15 ,16, 17, 18), `2` = structure(c(4L,5L, 2L, 5L, 2L, 3L, 1L, 6L,4L,5L, 2L, 5L, 2L, 3L, 1L, 6L,4L,5L),  .Label=c("a","a","b","b","b","c","c","b","b","b","e","e","f","g","g","g","f","f"),
class="factor"),`3`=c(1,0,1,1,0,1,0,1,1,0,1,0,1,1,0,1,0,0),`4`=c(0,1,0,0,1,0,0,0,0,1,0,1,0,0,0,0,1,1), `5` =c(10,5,20,20,5,0,0,10,10,5,10,5,15,5,5,5,2,2)),
 .Names = c("N", "Condition", "AOI_hit_b", "AOI_hit_f", "Time"), class = "data.frame", 
row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9","10","11","12","13","14","15","16","17","18"))

我现在想要在时间上进行计算,具体取决于Condition b前面是a还是c - 取决于AOI_hit_b是什么01。为此,只有1Condition的第一个匹配与时间相关,因为它总是在条件中为多个1写入相同的时间。 我尝试使用ddply包,但没有得到我想要的输出。 我的输出应该是这样的:

sum of 
Condition Time
b(a)       25
b(c)       10

这是我到目前为止的代码:

hit_cb = list()
for (i in 1:nrow(df)){
    if (df[i,2] == "b") & (df[i-1,2] == "c") {
        hit_cb[i] = ddply(df,.(AOI_hit_b), summarize, mysum=sum(unique(Time)))
    }
}    

0 个答案:

没有答案