0

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");
divibisan
  • 8,631
  • 11
  • 31
  • 46
RalfB
  • 533
  • 5
  • 19

1 Answers1

2

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
alexis_laz
  • 12,198
  • 3
  • 24
  • 33
  • My real dataframe contains 10000s of terms and 100 of categories, but i only have 5 terms i want to merge. Is there a way to enter the levels less explicit? – RalfB Mar 23 '14 at 23:33
  • @user2377039 : Well, what you did to "relevel" `Term` is possibly the most suitable way to go. I -just- added the `aggregate` step (as a late-night rep-seeking) :-). – alexis_laz Mar 24 '14 at 00:47
  • It worked out with the aggregate function in the end. Thank you. Your late night rep seeking has payed off, alexis_laz ;) – RalfB Mar 24 '14 at 12:51