【问题标题】:sum times dt within time ranges时间范围内的和时间 dt
【发布时间】:2014-02-21 16:53:39
【问题描述】:

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

头(希望)
次用户信号日志diff
1 2014-01-13 00:00:16 00250902DC7D 真正开启 31
2 2014-01-13 00:00:47 00250902DC7D 真正开启 31
3 2014-01-13 00:01:18 00250902DC7D 真正开启 30
4 2014-01-13 00:01:48 00250902DC7D 真正开启 31
5 2014-01-13 00:02:19 00250902DC7D 真正开启 31
6 2014-01-13 00:02:50 00250902DC7D 真正开启 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 个不同的事件......
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 个不同的事件执行此操作,我应该使用循环吗?

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

【问题讨论】:

    标签: r sum plyr


    【解决方案1】:

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

    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))
    

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 2012-09-18
      • 1970-01-01
      • 2020-12-20
      • 2020-07-28
      相关资源
      最近更新 更多