Search code examples
raggregatetidyversesummarysummarize

Wide Format Summary in tidyverse


Hi I have a dataframe in wide format that is grouped by Site. Each column represents the abundance a different species(85 total). I am trying to summarize the dataframe to calculate the total number of individuals regardless of species in my data.

df.totals<- df %>% group_by(Site) %>% summarize (total = sum(6:91))

Solution

  • We can gather to 'long' format and then do the sum

    library(tidyverse)
    df %>% 
       select(Site, 6:91) %>%
       rownames_to_column("rn") %>% 
       gather(key, val, - Site, -rn) %>% 
       group_by(Site, rn) %>% 
       summarise(total  = sum(val))
    

    or another option without gathering would be

    df %>% 
       select(Site, 6:91) %>% 
       transmute(vs, Sum = reduce(.[2:ncol(.)], `+`)) %>%
       group_by(Site) %>% 
       summarise(Sum = sum(Sum))
    

    Using a reproducible example with mtcars

    mtcars %>% 
       select(vs, 4:6) %>% 
       transmute(vs, Sum = reduce(.[2:ncol(.)], `+`)) %>% 
       group_by(vs) %>% 
       summarise(Sum = sum(Sum))