R:计算时差(分组)



我有以下样本数据

df <- data.frame(id = c(1,1,2,2,3,3),
times = c("2021-05-20 07:10:20", "2021-05-20 07:13:20", "2021-05-20 07:20:20", "2021-05-20 07:30:20", "2021-05-20 07:05:20", "2021-05-20 07:07:20"),
var1 = c("A", "B", "A", "B", "A", "B")) %>%
mutate(times = as.POSIXct(times,format="%Y-%m-%d %H:%M:%OS"))

我的目标是为每个ID添加一个额外的列,其中包含A和B之间的时间差(以秒或分钟为单位(。有人能帮帮我吗?

它应该是这样的:

id               times var1  duration_in_sec
1  1 2021-05-20 07:10:20    A  NA
2  1 2021-05-20 07:13:20    B  180
3  2 2021-05-20 07:20:20    A  NA
4  2 2021-05-20 07:30:20    B  600
5  3 2021-05-20 07:05:20    A  NA
6  3 2021-05-20 07:07:20    B  120

您可以使用以下解决方案:

library(dplyr)
df %>% 
group_by(id) %>%
mutate(diff_time = difftime(times, lag(times), units = "secs"))

# A tibble: 6 x 4
# Groups:   id [3]
id times               var1  diff_time
<dbl> <dttm>              <chr> <drtn>   
1     1 2021-05-20 07:10:20 A      NA secs 
2     1 2021-05-20 07:13:20 B     180 secs 
3     2 2021-05-20 07:20:20 A      NA secs 
4     2 2021-05-20 07:30:20 B     600 secs 
5     3 2021-05-20 07:05:20 A      NA secs 
6     3 2021-05-20 07:07:20 B     120 secs 

使用data.table

library(data.table)
setDT(df)[, diff_time := difftime(times, shift(times), units = 'secs'), id]

最新更新