【问题标题】:Calculating time in minutes between two dates in R (POSIXct format)以 R (POSIXct 格式)计算两个日期之间的时间(以分钟为单位)
【发布时间】:2021-09-16 06:27:19
【问题描述】:

我想计算 R 中两个事件之间的时间(以分钟为单位),仅针对特定 id(例如 892530)以及事件“eventBegins”和第一个 human2 事件之间(或者,另一种方法写这将是human2的最短日期时间)。请注意,“日期时间”变量采用 POSIXct 格式。我似乎无法使用 dplyr 和 base R (min()) 的混合来做到这一点,并且想为解决方案挑选你的大脑。最后,我试图获取数据集中每个 id 的平均时间差。

这是使用代码dput(head(df, 30)) 生成的输出 :

structure(list(visitor_id = c(175464, 175464, 175464, 892530, 
892530, 892530, 892530, 892530, 892530, 1006916, 1006916, 1006916, 
1336852, 1336852, 1336852, 2370624, 2370624, 2370624, 3347200, 
3347200, 3347200, 4539320, 4539320, 4539320, 4539320, 4539320, 
4666936, 4666936, 4666936, 4697670), event_type = c("human1", "human1", 
"human2", "human1", "eventBegins", "human1", "human2", "human2", "eventEnds", "human1", 
"human1", "human1", "human1", "human1", "human1", "human1", "human1", "human1", "human1", 
"human1", "human1", "human1", "eventBegins", "human2", "human2", "human1", "human1", 
"human1", "human1", "human1"), datetime = structure(c(1618678444, 
1618678444, 1618678444, 1617980667, 1617980668, 1617980668, 1617980668, 
1617980679, 1617980679, 1617530138, 1617530138, 1617530138, 1617299837, 
1617299837, 1617299837, 1617621792, 1617621792, 1617621792, 1618145874, 
1618145874, 1618145874, 1619013964, 1619013964, 1619013964, 1619014004, 
1619014005, 1617282418, 1617282418, 1617282418, 1619543098), class = c("POSIXct", 
"POSIXt"), tzone = "UTC")), row.names = c(NA, 30L), class = "data.frame") 

如果您对如何解决此问题有任何想法,请告诉我 - 我的想法已经不多了。 TIA。

【问题讨论】:

  • 你能不能这么好心,dput()你的数据框并在这里发布。
  • 如果您创建一个小的可重现示例以及预期的输出,这将更容易提供帮助。阅读how to give a reproducible example。
  • 谢谢!这里有点新手,但我会看看并尝试改进我的帖子以使其更清晰。
  • 您能否更清楚地了解“我正在尝试获取数据集中每个 id 1 的时间差的平均值”。你想获得所有人类的每个 id 的平均值还是只为human2?。写一两个方程式通常会很有帮助,这样可以使其更加具体并避免歧义。 :)
  • 在你的例子中可能是这样的(目前对我不起作用): df %>% group_by(id) %>% mutate(event_begins_per_id = min(datetime[event == "eventBegins "]), time_diff = datetime - event_begins_per_id, avg_time_diff_per_id = mean(time_diff[!event %in% c("eventBegins", "eventEnds")])) %>% summarise(mean_time_diff = mean(avg_time_diff_per_id, na.rm = TRUE )) 虽然我认为由于 'group_by(id)' 而无法获得所有 id 的平均值?

标签: r datetime time


【解决方案1】:

编辑:使用来自 OP 的示例数据。

cmets:您认为您可以使用以下方法吗?如果没有,请尝试(手动)编写具有预期结果的数据框。

library(tidyverse)

df %>% group_by(visitor_id) %>% 
  mutate(event_begins_per_id = min(datetime[event_type == "eventBegins"]), 
         time_diff = datetime - event_begins_per_id, 
         avg_time_diff_per_id = mean(time_diff[!event_type %in% c("eventBegins", "eventEnds")])) 

结果

# A tibble: 30 x 6
# Groups:   visitor_id [9]
   visitor_id event_type  datetime            event_begins_per_id time_diff avg_time_diff_per_id
        <dbl> <chr>       <dttm>              <dttm>              <drtn>    <drtn>              
 1     175464 human1      2021-04-17 16:54:04 NA NA               -Inf secs  -Inf secs          
 2     175464 human1      2021-04-17 16:54:04 NA NA               -Inf secs  -Inf secs          
 3     175464 human2      2021-04-17 16:54:04 NA NA               -Inf secs  -Inf secs          
 4     892530 human1      2021-04-09 15:04:27 2021-04-09 15:04:28   -1 secs  2.50 secs          
 5     892530 eventBegins 2021-04-09 15:04:28 2021-04-09 15:04:28    0 secs  2.50 secs          
 6     892530 human1      2021-04-09 15:04:28 2021-04-09 15:04:28    0 secs  2.50 secs          
 7     892530 human2      2021-04-09 15:04:28 2021-04-09 15:04:28    0 secs  2.50 secs          
 8     892530 human2      2021-04-09 15:04:39 2021-04-09 15:04:28   11 secs  2.50 secs          
 9     892530 eventEnds   2021-04-09 15:04:39 2021-04-09 15:04:28   11 secs  2.50 secs          
10    1006916 human1      2021-04-04 09:55:38 NA NA               -Inf secs  -Inf secs          
11    1006916 human1      2021-04-04 09:55:38 NA NA               -Inf secs  -Inf secs          
12    1006916 human1      2021-04-04 09:55:38 NA NA               -Inf secs  -Inf secs          
13    1336852 human1      2021-04-01 17:57:17 NA NA               -Inf secs  -Inf secs          
14    1336852 human1      2021-04-01 17:57:17 NA NA               -Inf secs  -Inf secs          
15    1336852 human1      2021-04-01 17:57:17 NA NA               -Inf secs  -Inf secs          
16    2370624 human1      2021-04-05 11:23:12 NA NA               -Inf secs  -Inf secs          
17    2370624 human1      2021-04-05 11:23:12 NA NA               -Inf secs  -Inf secs          
18    2370624 human1      2021-04-05 11:23:12 NA NA               -Inf secs  -Inf secs          
19    3347200 human1      2021-04-11 12:57:54 NA NA               -Inf secs  -Inf secs          
20    3347200 human1      2021-04-11 12:57:54 NA NA               -Inf secs  -Inf secs          
21    3347200 human1      2021-04-11 12:57:54 NA NA               -Inf secs  -Inf secs          
22    4539320 human1      2021-04-21 14:06:04 2021-04-21 14:06:04    0 secs 20.25 secs          
23    4539320 eventBegins 2021-04-21 14:06:04 2021-04-21 14:06:04    0 secs 20.25 secs          
24    4539320 human2      2021-04-21 14:06:04 2021-04-21 14:06:04    0 secs 20.25 secs          
25    4539320 human2      2021-04-21 14:06:44 2021-04-21 14:06:04   40 secs 20.25 secs          
26    4539320 human1      2021-04-21 14:06:45 2021-04-21 14:06:04   41 secs 20.25 secs          
27    4666936 human1      2021-04-01 13:06:58 NA NA               -Inf secs  -Inf secs          
28    4666936 human1      2021-04-01 13:06:58 NA NA               -Inf secs  -Inf secs          
29    4666936 human1      2021-04-01 13:06:58 NA NA               -Inf secs  -Inf secs          
30    4697670 human1      2021-04-27 17:04:58 NA NA               -Inf secs  -Inf secs          

【讨论】:

  • 谢谢你!该代码似乎不起作用,因为它返回“将 Infno 非缺失参数返回给 min” - 这是因为 POSIXct 格式吗?为了回答你上面的问题,我想要 eventBegins 和第一个(最小时间)human2 为 id '1' 之间的时间差——所以第 4 行的时间减去上面小标题中第 2 行的时间。然后我想通过找出第一个“人类 2”(未显示)和 eventBegins(第 10 行)之间的差异来对 id 2 做同样的事情。然后我想计算所有 id(1、2 等)的时间差的平均值。抱歉,如果不清楚。
  • 也不确定如何修改我的代码以使其可重现...:/我是 R(& Stack Overflow)新手,但感谢您的快速回复!下次会查看视频,向我展示如何改进我的帖子。
  • 无需道歉 :) 以使其可重现,假设它被称为 df,为数据框的前 30 行运行 dput(head(df, 30)。然后编辑您的问题并在最后附加dput(...) 的输出到您的问题。有了这个,我将能够处理您的数据样本。澄清一下:为什么不应该考虑第 9 行(id=2)中的human2?因为它出现在“事件开始”之前?因此,对于所有 id,逻辑只采用 human2 AFTER eventbegins?
  • 好的,感谢您提供详细信息!我已按照建议运行 dput() 并将输出附加到我的问题中。是的,我只想知道事件“eventBegins”和“human2”第一次出现(最早或最短时间)之间经过的时间(以分钟为单位)。所以是的,第 9 行的 human2 不会被视为出现在上面示例中 id 2 的第一个“eventBegins”之前。我想知道您的示例中缺少什么阻止代码处理数据(并给了我上面的错误)?我想明白。谢谢!
  • “no non-missing”警告是因为一些 visitor_ids 是唯一的并且没有与之相关的“eventBegins”(例如 id 175464),但这只是警告并且会导致缺失值。
猜你喜欢
  • 2011-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-07
  • 2020-01-02
  • 2011-04-19
  • 2021-05-12
  • 2012-01-03
相关资源
最近更新 更多