I would like to pool the data from two different variables and sum it up. The original dataframe looks like this:
group animal period zone day cum_time_sec
<chr> <chr> <chr> <chr> <dbl> <dbl>
1 chr chr1 q1 eating 1 3.22
2 chr chr1 q1 interzone 1 2.14
3 chr chr1 q1 marble 1 4.26
4 chr chr1 q1 nest 1 0.624
I would like to pool the data from nest and marble and collapse them into a single row. Using recode I got to this:
group animal period zone day cum_time_sec
<chr> <chr> <chr> <chr> <dbl> <dbl>
1 chr chr1 q1 eating 1 3.22
2 chr chr1 q1 interzone 1 2.14
3 chr chr1 q1 nest 1 4.26
4 chr chr1 q1 nest 1 0.624
but what I'm looking for is this:
group animal period zone day cum_time_sec
<chr> <chr> <chr> <chr> <dbl> <dbl>
1 chr chr1 q1 eating 1 3.22
2 chr chr1 q1 interzone 1 2.14
3 chr chr1 q1 nest 1 4.884
Would be very grateful for some help. Thank you :)
CodePudding user response:
Without a minimal reproducible example it's kind of hard to be sure, but this should work:
library(dplyr)
yourdata %>%
mutate(zone = ifelse(zone == "marble", "nest", zone)) %>%
group_by(group, animal, period, zone, day) %>%
summarise(cum_time_sec = sum(cum_time_sec)) %>%
ungroup()