我对应该如何进行感到困惑。我下面有一些虚拟数据:
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
对于每个人,我想确定在每个站点花费的时间。是否有一个功能可以根据这两个条件汇总花费的时间?任何帮助或投入,我们将不胜感激。
答案 0 :(得分:1)
如何?
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创建
答案 1 :(得分:0)
我愿意...
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