Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Summarizing two variables into one

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  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 🙂

>Solution :

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading