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.
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)