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.)
After the
summarise
, we need toungroup
It can also be written by first doing the
group_by
sum
and thentransmute
Or another option is
sum
withmap
data