如何在ddply中使用函数循环

时间:2022-03-13 22:55:44

I would like to sum a column of time differences diff for 14 different users when these time differences occurred in some fixed time events(number of events=108).
here the head of the first dataframe with times differences 'diff`, this dataframe contains 152171 rows:

我想在一些固定时间事件中发生这些时差(事件数量= 108)时,为14个不同的用户总结一列时差diff。这里是第一个数据帧的头部,时间差为'diff`,这个数据帧包含152171行:

head(hope)
                times        users signal mode diff
1 2014-01-13 00:00:16 00250902DC7D   true   ON   31
2 2014-01-13 00:00:47 00250902DC7D   true   ON   31
3 2014-01-13 00:01:18 00250902DC7D   true   ON   30
4 2014-01-13 00:01:48 00250902DC7D   true   ON   31
5 2014-01-13 00:02:19 00250902DC7D   true   ON   31
6 2014-01-13 00:02:50 00250902DC7D   true   ON   31 

The second dataframe with 108 different times ranges (nrow=108)is:

具有108个不同时间范围(nrow = 108)的第二个数据帧是:

head(events)
          start                 end
1 2014-01-14 06:30:00 2014-01-14 07:00:00
2 2014-01-14 10:30:00 2014-01-14 11:00:00
3 2014-01-14 18:00:00 2014-01-14 18:30:00
4 2014-01-14 22:30:00 2014-01-14 22:59:00
5 2014-01-15 02:30:00 2014-01-15 02:59:00
6 2014-01-15 09:00:00 2014-01-15 09:30:00  

If I select one event manually (I chose by chance the 12th event..), I able to count the timedifferences (diff) within the 12th event and it works...but I have 108 different evevnts...

如果我手动选择一个事件(我偶然选择了第12个事件......),我可以计算第12个事件中的时差(差异)并且它有效...但我有108个不同的evevnts ...

hope1 <- hope[hope$mode=="ON" & hope$times>events[12,1] & hope$times<events[12,2],]
ddply(hope1,.(users),summarize,sum=sum(diff))  
         users  sum
1 00250902DC7D 1857
2 00250902FA92 1857
3 00250902FB05 1857
4 002509030C41 1857
5 002509030E53 1857  

*ok perfect, BUT ONLY FOR ONE EVENT*

*确定完美,但仅限一次活动*

If I want to do it for 108 different events, should I use a loop maybe?

如果我想为108个不同的事件做,我应该使用循环吗?

I tried something like the following code, but I/it failed...:

我试过类似下面的代码,但我/它失败了......:

> for (i in 1:108)
+ hope5 <- data.frame(hope[hope$mode=="ON" & hope$times>events[i,1] & hope$times<events[i,2],])  
ddply(hope5,.(users),summarize,sum=sum(diff))

Could you help me please?

请问你能帮帮我吗?

I would like to get an output like this one:

我想得到像这样的输出:

> pippo

                   00250902DC7D 00250902FA92 00250902FB05
2014-01-14 06:30:00           35           32          335
2014-01-14 10:30:00           38           31          338
2014-01-14 18:00:00           49           29          429
2014-01-14 22:30:00           48          438           48
2014-01-15 02:30:00           29           29          289

1 个解决方案

#1


1  

You could work with list and lapply :

你可以使用list和lapply:

hopeN <- lapply(1:nrow(events), function(i) hope[hope$mode=="ON" & hope$times>events[i,1] & hope$times<events[i,2],])

result <- lapply(1:length(hopeN), function(i) ddply(hopeN[[i]],.(users),summarize,sum=sum(diff)))

The result is a list of data.frames.

结果是data.frames列表。

#1


1  

You could work with list and lapply :

你可以使用list和lapply:

hopeN <- lapply(1:nrow(events), function(i) hope[hope$mode=="ON" & hope$times>events[i,1] & hope$times<events[i,2],])

result <- lapply(1:length(hopeN), function(i) ddply(hopeN[[i]],.(users),summarize,sum=sum(diff)))

The result is a list of data.frames.

结果是data.frames列表。