问题
I'm stumped with how I should proceed. I have some dummy data below:
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)
Producing this data frame:
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
For each individual, I would like to determine the time spent at each site. Is there a function that will summarize time spent according to the two conditions? Any help or input is much appreciated.
回答1:
How about this:
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
Created on 2019-03-20 by the reprex package (v0.2.1)
回答2:
I'd do...
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
or similarly in dplyr verbs:
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
来源:https://stackoverflow.com/questions/55268381/aggregate-date-time-to-summarize-time-spent-at-certain-conditions