Hello,
I have a dataset with samples that require summation depending on where the sample was taken. The depth classes of the sample are "S", "D", "I," and "DS". Within the same transect (group), I would like to create a column that sums the volumes of D and DS.
For example, for transect stj38
, the volumes with categories D (vol ~ 2.78) and DS (vol ~ 3.04) would sum to ~ 5.82 in the NEW_VOLUME
column.
I'm guessing I would use the group_by()
and summarize()
or mutate()
functions, as shown below, but I'm not sure where to go from there.
dtnew <- dt %>%
group_by(TRANSECT) %>%
mutate(NEW_VOLUME = function(x))
Or something like that. I don't know what syntax to use to describe how to sum the different DEPTH_CLASS
categories.
Any help would be greatly appreciated.
Here's a snippet of my data:
dt <- structure(list(TRANSECT = c("stj17", "stj17", "stj38", "stj38",
"stj38", "stj30", "stj30", "stj30", "stj23", "stj23"), SERIAL = c("166",
"167", "177", "178", "179", "186", "187", "188", "195", "196"
), Volume_m3 = c(2.35845643690293, 14.7035018488167, 3.04633956433295,
2.78838339154669, 4.63092748287711, 8.73365899290616, 9.51981113854047,
6.00669373773715, 9.69178192039798, 9.188153202101), DEPTH_CLASS = c("S",
"D", "DS", "D", "S", "DS", "D", "S", "DS", "D")), row.names = c(NA,
10L), class = "data.frame")
Thanks so much!