0

I try to do simple aggregate in R by this code:

test <- data.frame(a=c("x","y","x","y"), 
                  b=c(2,3,4,5))
test2 <-aggregate(test, by=list(test$a),FUN="sum", na.rm=TRUE)

Dataset test looks like this:

  a b
1 x 2
2 y 3
3 x 4
4 y 5

And I want the answer test2 to be:

  a b
1 x 6
2 y 8

I get an error message:

Error in Summary.factor(c(1L, 1L), na.rm = TRUE) : 
  ‘sum’ not meaningful for factors

Any ideas where I went wrong?

Parallax
  • 41
  • 6

1 Answers1

0

Use this

test2 <- aggregate(data=test,b~.,FUN = sum)
user2100721
  • 3,557
  • 2
  • 20
  • 29