I Del Toro
I Del Toro

Reputation: 943

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

Upvotes: 0

Views: 515

Answers (1)

akrun
akrun

Reputation: 887118

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

Upvotes: 1

Related Questions