How to sum a variable by factor levels in R?

R ProgrammingServer Side ProgrammingProgramming

We can do this by using aggregate function or with the help tapply

Example

> x <- data.frame(Category=factor(c("Graduation", "Graduation", "Post-Graduation",
"Graduation","PhD", "Post-Graduation", "PhD")),
Frequency=c(12,19,15,20,25,13,14))
> x
Category Frequency
1 Graduation 12
2 Graduation 19
3 Post-Graduation 15
4 Graduation 20
5 PhD 25
6 Post-Graduation 13
7 PhD 14

Using aggregate

> aggregate(x$Frequency, by=list(Group=x$Category), FUN=sum)
    Group x
1 Graduation 51
2 PhD 39
3 Post-Graduation 28
Using tapply
> tapply(x$Frequency, x$Category, FUN=sum)
Graduation PhD Post-Graduation
51         39      28
raja
Published on 06-Jul-2020 14:07:12
Advertisements