Search code examples
rr-factor

R: Merging factor levels and creating sum in merged columns


I am a beginner in R and this is driving me nuts.

I have a dataframe:

someData = data.frame(Term=c('a', 'b', 'c', 'd', 'a', 'a', 'c', 'c'), Freq=c(1:8), Category=c(1,2,1,2,1,2,1,2));
someData$Term = as.factor(someData$Term)
someData$Category = as.factor(someData$Category)

and would like to combine terms a and c (both factors) into x while summing their respective frequencies and while maintaining the categories so that I have the following resulting dataframe:

Term Freq Category
x    16       1
b    2        2
d    4        2
x    14       2

The following code only changes all names to x, but does not sum its values.

combine <- c("a", "c");
levels(somedata$Term)[levels(somedata$Term) %in% combine] <- paste("x");

Solution

  • This looks valid:

    #levels(someData$Term) = list(b = "b", d = "d", x = c("a", "c")) #just another approach
    aggregate(Freq ~ Term + Category, someData, sum)
    #  Term Category Freq
    #1    x        1   16
    #2    b        2    2
    #3    d        2    4
    #4    x        2   14