总时间dt在时间范围内

时间:2014-02-21 16:53:39

标签: r sum plyr

如果在某些固定时间事件中发生这些时差,我想为14位不同的用户总结一列时差diff
这里是第一个数据帧的头部,带有时间差'diff`,这个数据帧包含152171行:

  

头(希望)
               次用户信号记录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

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

          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

如果我手动选择事件(我偶然选择了第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  

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

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

你能帮帮我吗? 有人活着吗?

1 个答案:

答案 0 :(得分:0)

我认为这可能是你的起点。

head(hope)
hope <- read.table(text="times users signal log 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", sep="", header=F,skip=1)

head(hope)
hope$V1 <- NULL
names(hope) <- c("date","time", "users","signal","log","diff")

hope$datetime <- as.POSIXct(strptime(as.character(paste(hope$date,hope$time)),
                          format="%Y-%m-%d %H:%M:%S"))
hope <- hope[,c(7,3,4,5,6)]

hope
library(plyr)

# Define dates where you want to find the sum
# I chose different dates that the date you gave because I didn't have enough data to test
hope1 <- subset(hope, (datetime  >as.POSIXct(c("2014-01-13 00:01:18")) & 
                                  datetime <as.POSIXct("2014-01-13 00:02:50")))
# Find the sum
sum(hope1$diff)

# Find the sum by users 
ddply(hope1,.(users),summarize,sum=sum(diff))

我希望这会有所帮助。