Sum data based on categorical criteria


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!

Is this what you want?

dt %>%
filter(DEPTH_CLASS != "S") %>%
group_by(TRANSECT) %>%
summarize(s = sum(Volume_m3))

A tibble: 4 x 2


1 stj17 14.7
2 stj23 18.9
3 stj30 18.3
4 stj38 5.83

1 Like

That actually works well with the mutate function, summarize seems to collapse all the rest of the data. Thank you so much for your help!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.