日期时间对象上的差异在分组的小标题上使用时会产生意外的结果

时间:2019-07-02 22:59:02

标签: r datetime dplyr posixct

我有一个数据框(或小标题),其中每一行在列datetime中都有一个POSIXct对象。我想按列event将此df分组,然后使用该事件的最早和最晚日期时间之间的差异来计算每个事件的持续时间。使用dplyr :: group_by(event)会导致基本R时间差产生错误的结果。

使用整洁的语法和dplyr,我可以按事件过滤数据帧并获得期望的结果。但是,使用group_by计算数据帧中每个事件的持续时间,产生的时间差是不正确的。

奇怪的是,它是相同的数值,但单位不正确。

library(dplyr)

df = structure(list(datetime = structure(c(1113573660, 1115025600, 
                                           1120784400, 1120788060, 1120791600, 1120795200, 1120798800, 1120802400, 
                                           1120806000, 1120809600, 1120813200, 1120816800, 1120820460, 1120824000, 
                                           1120827600, 1120831200, 1120834860, 1120838400, 1120842000, 1120860060, 
                                           1121034660, 1121113800, 1121121600, 1121122200, 1121122800, 1121482800, 
                                           1121486400, 1121490000, 1121493660, 1121497260, 1121504460, 1121508000, 
                                           1121511600, 1121515200), class = c("POSIXct", "POSIXt"), tzone = ""), 
                    event = c(1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
                              3, 3, 3, 4, 5, 6, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8)), row.names = c(NA, 
                                                                                                    -34L), sf_column = "geometry", agr = structure(c(datetime = NA_integer_, 
                                                                                                                                                     AnimalID = NA_integer_, DOP = NA_integer_, SV = NA_integer_, 
                                                                                                                                                     HU = NA_integer_, Sex = NA_integer_, in_core = NA_integer_, exit_event = NA_integer_
                                                                                                    ), .Label = c("constant", "aggregate", "identity"), class = "factor"), class = "data.frame")
df = df %>% as_tibble()

head(df)
#> # A tibble: 6 x 2
#>   datetime            event
#>   <dttm>              <dbl>
#> 1 2005-04-15 07:01:00     1
#> 2 2005-05-02 02:20:00     2
#> 3 2005-07-07 18:00:00     3
#> 4 2005-07-07 19:01:00     3
#> 5 2005-07-07 20:00:00     3
#> 6 2005-07-07 21:00:00     3

# correct result
df %>% 
  filter(event == 3) %>%
  summarise(event = first(event), 
            duration = max(datetime) - min(datetime)) 
#> # A tibble: 1 x 2
#>   event duration
#>   <dbl> <drtn>  
#> 1     3 16 hours

# correct result
df %>% 
  group_by(event) %>% 
  summarise(duration = difftime(max(datetime), min(datetime), 
                                units = "secs")) %>%
  filter(event == 3)
#> # A tibble: 1 x 2
#>   event duration  
#>   <dbl> <drtn>    
#> 1     3 57600 secs

# incorrect result
df %>% 
  group_by(event) %>% 
  summarise(duration = max(datetime) - min(datetime)) %>%
  filter(event == 3)
#> # A tibble: 1 x 2
#>   event duration
#>   <dbl> <drtn>  
#> 1     3 16 secs

reprex package(v0.3.0.9000)于2019-07-02创建

没有返回错误,但是上一次duration计算的结果应该是“ 16小时”或“ 57600秒”。而是“ 16秒”

0 个答案:

没有答案