How to obtain species richness and abundance for sites with multiple samples using dplyr

℡╲_俬逩灬. 提交于 2019-12-24 00:56:57

问题


Problem:

I have a number of sites, with 10 sampling points at each site.

Site Time Sample Species1 Species2 Species3 etc
Home    A      1        1        0        4 ...
Home    A      2        0        0        2 ...
Work    A      1        0        1        1 ...
Work    A      2        1        0        1 ...
Home    B      1        1        0        4 ...
Home    B      2        0        0        2 ...
Work    B      1        0        1        1 ...
Work    B      2        1        0        1 ...
...

I would like to obtain the richness and abundance of each site. Richness is the total number of species at a site, and abundance is the total number of all individuals of all species at a site, like this:

Site Time Richness Abundance
Home    A        2         7
Work    A        3         4
Home    B        2         7
Work    B        3         4

I can get there with two functions (below). However, I would like both in one dplyr function. The range 7:34 refers to my species matrix (each row a site/sample, species as columns).

df1 <- df %>% mutate(Abundance = rowSums(.[,4:30])) %>%
group_by(Site,Time) %>%   
    summarise_all(sum)

df1$Richness <- apply(df1[,4:30]>0, 1, sum)

If I try to do both in one function, I get the following error

df1 <- df  %>% mutate(Abundance = rowSums(.[,4:30]) ) %>%
   group_by(Site, Time) %>%   
   summarise_all(sum) %>% 
   mutate(Richness = apply(.[,4:30]>0, 1, sum))

Error in mutate_impl(.data, dots) : 
  Column `Richness` must be length 5 (the group size) or one, not 19

The Richness part has to come after the summarise function, since it has to operate on summed and grouped data.

How do I make this function work?

(Note: This was previously marked as a duplicate of this question: Manipulating seperated species quantity data into a species abundance matrix

It is a completely different question, however - that question is essentially about transposing a dataset and summing within a single species/column. This is about summing all species across columns (multiple columns). In addition, I actually think the answer to this question is very helpful - ecologists like me calculate richness and abundance all the time, and I'm sure they'll appreciate a dedicated question.)


回答1:


After the summarise, we need to ungroup

library(tidyverse)
df %>% 
  mutate(Abundance = rowSums(.[4:ncol(.)])) %>% 
  group_by(Site, Time) %>% 
  summarise_all(sum) %>%
  ungroup %>% 
  mutate(Richness = apply(.[4:(ncol(.)-1)] > 0, 1, sum)) %>%
  #or
  #mutate(Richness = rowSums(.[4:(ncol(.)-1)] > 0)) %>%
  select(Site, Time, Abundance, Richness)
# A tibble: 4 x 4
#  Site  Time  Abundance Richness
#  <chr> <chr>     <dbl>    <int>
#1 Home  A             7        2
#2 Home  B             7        2
#3 Work  A             4        3
#4 Work  B             4        3

It can also be written by first doing the group_by sum and then transmute

df %>% 
  group_by(Site, Time) %>%
  summarise_at(vars(matches("Species")), sum)  %>% 
  ungroup %>%
  transmute(Site, Time, Abundance = rowSums(.[3:ncol(.)]), 
                        Richness =  rowSums(.[3:ncol(.)] > 0))

Or another option is sum with map

df %>% 
   group_by(Site, Time) %>%
   summarise_at(vars(matches("Species")), sum) %>% 
   group_by(Time, add = TRUE) %>%
   nest %>% 
   mutate(data = map(data, ~ 
                 tibble(Richness = sum(.x > 0), 
                        Abundance = sum(.x)))) %>% 
   unnest

data

df <- structure(list(Site = c("Home", "Home", "Work", "Work", "Home", 
"Home", "Work", "Work"), Time = c("A", "A", "A", "A", "B", "B", 
"B", "B"), Sample = c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), Species1 = c(1L, 
0L, 0L, 1L, 1L, 0L, 0L, 1L), Species2 = c(0L, 0L, 1L, 0L, 0L, 
0L, 1L, 0L), Species3 = c(4L, 2L, 1L, 1L, 4L, 2L, 1L, 1L)), 
class = "data.frame", row.names = c(NA, 
 -8L))


来源:https://stackoverflow.com/questions/52547792/how-to-obtain-species-richness-and-abundance-for-sites-with-multiple-samples-usi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!