聚合日期时间以总结在特定条件下花费的时间
Aggregate date time to summarize time spent at certain conditions
我不知道该如何继续。我在下面有一些虚拟数据:
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)
正在生成此数据框:
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
对于每个人,我想确定在每个站点花费的时间。有没有函数可以根据两种情况汇总所花费的时间?非常感谢任何帮助或输入。
这个怎么样:
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
由 reprex package (v0.2.1)
于 2019-03-20 创建
我愿意...
library(data.table)
setDT(DF)
spellDT = DF[, .(StartDate = first(Date)), by=.(Individual, Site = Sites, g = rleid(Sites))]
spellDT[, duration := shift(StartDate, type="lead") - StartDate, by=Individual][]
Individual Site g StartDate duration
1: A 4 1 2018-03-20 11:52:25 NA mins
2: B 6 2 2018-03-20 12:10:40 2.183333 mins
3: B 7 3 2018-03-20 12:12:51 NA mins
或类似的 dplyr 动词:
library(dplyr)
DF %>% distinct(Individual, g = data.table::rleid(Sites), .keep_all = TRUE) %>%
rename(StartDate = Date, Site = Sites) %>%
group_by(Individual) %>% mutate(duration = lead(StartDate) - StartDate)
# A tibble: 3 x 5
# Groups: Individual [2]
Individual StartDate Site g duration
<fct> <dttm> <dbl> <int> <time>
1 A 2018-03-20 11:52:25 4 1 NA mins
2 B 2018-03-20 12:10:40 6 2 2.183333 mins
3 B 2018-03-20 12:12:51 7 3 NA mins
我不知道该如何继续。我在下面有一些虚拟数据:
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)
正在生成此数据框:
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
对于每个人,我想确定在每个站点花费的时间。有没有函数可以根据两种情况汇总所花费的时间?非常感谢任何帮助或输入。
这个怎么样:
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
由 reprex package (v0.2.1)
于 2019-03-20 创建我愿意...
library(data.table)
setDT(DF)
spellDT = DF[, .(StartDate = first(Date)), by=.(Individual, Site = Sites, g = rleid(Sites))]
spellDT[, duration := shift(StartDate, type="lead") - StartDate, by=Individual][]
Individual Site g StartDate duration
1: A 4 1 2018-03-20 11:52:25 NA mins
2: B 6 2 2018-03-20 12:10:40 2.183333 mins
3: B 7 3 2018-03-20 12:12:51 NA mins
或类似的 dplyr 动词:
library(dplyr)
DF %>% distinct(Individual, g = data.table::rleid(Sites), .keep_all = TRUE) %>%
rename(StartDate = Date, Site = Sites) %>%
group_by(Individual) %>% mutate(duration = lead(StartDate) - StartDate)
# A tibble: 3 x 5
# Groups: Individual [2]
Individual StartDate Site g duration
<fct> <dttm> <dbl> <int> <time>
1 A 2018-03-20 11:52:25 4 1 NA mins
2 B 2018-03-20 12:10:40 6 2 2.183333 mins
3 B 2018-03-20 12:12:51 7 3 NA mins