Aggregating data values with same property

This is a simple question but for some reason it is giving me trouble. I have a dataset and there are categories that have multiple data vales. I want to sum these data values so that each category has only one data value. This is a reprex of the columns I want to sum. I've tried lapply but did not obtain the result I wanted

data.frame(category = c("x","x","x","x","x","y","y","y","y","y","z","z","z","z","z"),
 value = c(29,-30,48,58,-67,56,-32,75,61,32,-38,24,43,44,-56))

Thanks in advance!

You can use dplyr

library(dplyr) 
DF <- data.frame(category = c("x","x","x","x","x","y","y","y","y","y","z","z","z","z","z"),
 value = c(29,-30,48,58,-67,56,-32,75,61,32,-38,24,43,44,-56))
DF %>%
    group_by(category) %>%
    summarise(value = sum(value)) 
2 Likes

Thank you, I tried it and it works for my dataset!

A way to do this built into R is base::tapply().

d <- data.frame(
  category = c("x","x","x","x","x","y","y","y","y","y","z","z","z","z","z"),
  value = c(29,-30,48,58,-67,56,-32,75,61,32,-38,24,43,44,-56))
tapply(d$value, d$category, FUN = sum)
#  x   y   z 
# 38 192  17 
3 Likes

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