I am trying to figure out the most efficient way to achieve a series of goals to group my data, summarize columns, and mutate a new column based on the summary.
With the example data below, I want to:
- mutate a new column "sum", which would be the sum of "count" , group_by(site, trmt, id, species)
- calculate the relative abundance of each species, group_by(id).
This post comes close to helping me out, but I'm not trying to summarise(across()) multiple columns: dplyr: group_by, sum various columns, and apply a function based on grouped row sums?
How would you work through this with pipes in dplyr to get from ''df_have'' to ''df_want''?
Thank you!
site <- c("X", "Y", "Y", "X", "X", "X", "Y", "X", "Y", "X", "Y", "Y", "X", "X", "X", "Y", "X", "Y")
trmt <- c("yes", "yes", "no", "no", "yes", "no", "no", "yes", "yes", "yes", "yes", "no", "no", "yes", "no", "no", "yes", "yes")
id <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9)
species <- c("a", "b", "a", "c", "d", "a", "e", "b", "d", "a", "b", "m", "c", "p", "a", "q", "r", "d")
count <- c(28, 17, 7, 8, 2, 9, 1, 5, 3, 12, 4, 18, 3, 30, 12, 21, 18, 6)
extra <- c("A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B")
df_have <- cbind(site, trmt, id, species, count, extra)
df_have <- as.data.frame(df_have)
df_have
site1 <- c("X", "Y", "Y", "X", "X", "Y", "Y", "X", "X", "Y" )
trmt1 <- c("yes", "yes", "no", "yes", "no", "no", "no", "yes", "yes", "yes" )
id1 <- c(1, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8, 8, 9)
species1 <- c("a", "b", "a", "m", "c", "d", "p", "a", "e", "q", "b", "r", "d" )
sum <- c(40, 21, 7, 18, 11, 2, 30, 21, 1, 21, 5, 18, 9)
relabund <- c(100, 100, 38.9, 61.1, 100, 6.25, 93.75, 100, 4.54, 95.45, 27.74, 78.26, 100)
df_want <- cbind(site1, trmt1, id1, species1, sum, relabund)
df_want <- as.data.frame(df_want)
df_want
CodePudding user response:
Here is a dplyr
option
library(dplyr)
df_have %>%
group_by(site, trmt, id, species) %>%
summarise(sum = sum(as.integer(count)), .groups = "drop") %>%
group_by(id) %>%
mutate(relabund = sum / sum(sum) * 100) %>%
ungroup() %>%
arrange(id, species)
## A tibble: 13 x 6
# site trmt id species sum relabund
# <chr> <chr> <chr> <chr> <int> <dbl>
# 1 X yes 1 a 40 100
# 2 Y yes 2 b 21 100
# 3 Y no 3 a 7 28
# 4 Y no 3 m 18 72
# 5 X no 4 c 11 100
# 6 X yes 5 d 2 6.25
# 7 X yes 5 p 30 93.8
# 8 X no 6 a 21 100
# 9 Y no 7 e 1 4.55
#10 Y no 7 q 21 95.5
#11 X yes 8 b 5 21.7
#12 X yes 8 r 18 78.3
#13 Y yes 9 d 9 100
The last arrange()
command is just to match your expected output; if the order doesn't matter you can omit. Also note that the data in the count
column are character
s, so we need to convert to integer
first; this is something that should probably be fixed upstream.