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