简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM