汇总日期时间以总结在某些条件下花费的时间 [英] Aggregate date time to summarize time spent at certain conditions

查看:92
本文介绍了汇总日期时间以总结在某些条件下花费的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对应该如何进行感到困惑。我在下面有一些虚拟数据:

 日期<-as.POSIXct(c('2018-03-20 11:52 :25','2018-03-22 12:01:44','2018-03-20 12:05:25','2018-03-20 12:10:40','2018-03-20 12 :12:51'))
网站< -c(4、4、4、6、7)
个人< -c( A, A, A, B , B)

data.frame(个人,日期,站点)

产生此数据框:

 单个日期站点
A 2018-03-20 11:52:25 4
A 2018-03-20 12:01:44 4
A 2018-03-20 12:05:25 4
B 2018-03-20 12:10:40 6
B 2018-03-20 12:12:51 7

对于每个人,我想确定在每个站点花费的时间。是否有一个功能可以根据这两个条件汇总花费的时间?任何帮助或投入,我们将不胜感激。

解决方案

如何操作:





< pre class = lang-r prettyprint-override> library(tidyverse)

Date<-as.POSIXct(c('2018-03-20 11:52: 25','2018-03-22 12:01:44','2018-03-20 12:05:25','2018-03-20 12:10:40','2018-03-20 12: 12:51'))
网站<-c(4,4,4,6,7)
个人<-c( A, A, A, B , B)

df<-data.frame(单个,日期,站点)

df%&%;%
group_by(单个,站点)%>%
summarise(time_spent = max(Date)-min(Date))
#> #小动作:3 x 3
#> #组:个人[2]
#>单个网站time_spent
#> < fct> < dbl> <时间>
#> 1 A 4 2.00647天
#> 2 B 6 0.00000天
#> 3 B 7 0.00000天

reprex包(v0.2.1)


I'm stumped with how I should proceed. I have some dummy data below:

 Date <- as.POSIXct(c('2018-03-20 11:52:25', '2018-03-22 12:01:44', '2018-03-20 12:05:25', '2018-03-20 12:10:40', '2018-03-20 12:12:51 ')) 
Sites<-c(4, 4, 4, 6, 7)
Individual<-c("A", "A", "A", "B", "B")

data.frame(Individual, Date, Sites)

Producing this data frame:

  Individual                Date Sites
          A 2018-03-20 11:52:25     4
          A 2018-03-20 12:01:44     4
          A 2018-03-20 12:05:25     4
          B 2018-03-20 12:10:40     6
          B 2018-03-20 12:12:51     7

For each individual, I would like to determine the time spent at each site. Is there a function that will summarize time spent according to the two conditions? Any help or input is much appreciated.

解决方案

How about this:

library(tidyverse)

Date <- as.POSIXct(c('2018-03-20 11:52:25', '2018-03-22 12:01:44', '2018-03-20 12:05:25', '2018-03-20 12:10:40', '2018-03-20 12:12:51 ')) 
Sites <- c(4, 4, 4, 6, 7)
Individual <- c("A", "A", "A", "B", "B")

df <- data.frame(Individual, Date, Sites)

df %>%
  group_by(Individual, Sites) %>%
  summarise(time_spent = max(Date) - min(Date))
#> # A tibble: 3 x 3
#> # Groups:   Individual [2]
#>   Individual Sites time_spent  
#>   <fct>      <dbl> <time>      
#> 1 A              4 2.00647 days
#> 2 B              6 0.00000 days
#> 3 B              7 0.00000 days

Created on 2019-03-20 by the reprex package (v0.2.1)

这篇关于汇总日期时间以总结在某些条件下花费的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆