我有一个要分组的数据框,并以一致的单位获得diff(滞后差)的中位数。与下面的示例非常相似。如您所见,通过运行下面的代码,我遇到了问题,因为diff的summarise
函数没有考虑到units属性
library(tidyverse)
# Initialise random data
t = Sys.time()
rnd <- sample(1:10000,10,replace=F)
add <- rnd[order(rnd)]
# Create 2 dtaaframes
time1 <- data.frame(datetime = t + add)
time2 <- data.frame(datetime = t + add * 1000)
# Bind dataframe together
mydata <- bind_rows(time1, time2, .id = "group")
# Trying to get a summary table
mydata %>% group_by(group) %>% summarise(elapsed = median(diff(datetime[order(datetime)])))
# These are the values that I should get in my summary table
median(diff(time1$datetime))
median(diff(time2$datetime))
答案 0 :(得分:2)
这里是一个选项,它将在几秒钟内显示所有结果。如果更合适,请使用dminutes(1)
或dhours(1)
或ddays(1)
。
mydata %>%
group_by(group) %>%
summarise(elapsed = median(diff(datetime[order(datetime)])) / lubridate::dseconds(1))
答案 1 :(得分:2)
如何使用difftime
并设置单位?
mydata %>%
group_by(group) %>%
summarise(elapsed = median(difftime(datetime, lag(datetime), units = "mins"), na.rm = TRUE))