using aggregate() to group in two ways, error: 'sum' not meaningful for factors

I have a dataset that looks something like

group class  amount
1       1      5  
2       2      20
1       2      3
2       2      10

I want to group it in two ways, by both group and class.... for example group 1, has a total of 5 for class 1 and 3 for class 2..... while group 2 has a total of 30 for class 2.

I was trying to use aggregate but ran into an error: 'sum' not meaningful for factors.

aggregate(df$amount, list(df$class), FUN=sum)
Error in Summary.factor(c(320L, 1L, 123L, 1L, 90L, 302L, 51L, 150L, 335L, :
‘sum’ not meaningful for factors

So I have two problems, using aggregate() with two sorts of groups.... and also the factors problem. To address the factors error I tried using

df %>% mutate_if(class, is.factor, ~parse_number(as.character(.)))
and got
Error in is_logical(.p) : object 'class' not found

You've already asked a question regarding factors, so I'm skipping that question. If you continue to have that problem, please create a new thread with a reproducible example of how you are loading the data.

Regarding the aggregate question, I think this is what you want:

> sample_data <- read.table(text = "group class  amount
+ 1       1      5  
+ 2       2      20
+ 1       2      3
+ 2       2      10",
+                           header = TRUE)
> aggregate(formula = amount ~ group + class,
+           data = sample_data,
+           FUN = sum)
  group class amount
1     1     1      5
2     1     2      3
3     2     2     30

This solution of course assumes that the data are in fact numbers, and not factors. The way you used mutate_if is incorrect. Check Andres previous solution, and also read the docs. You should do something like this:

df %>% mutate_if(is.factor, ~parse_number(as.character(.)))

And, if you already using dplyr, you can consider to use group_by and summarise as well.

Hope this helps.

1 Like

Thank you, this is helpful. I used mutate_if the correct way and thought it was successful, yet now when I use the aggregate code, I am getting

Error in FUN(X[[i]], ...) : invalid 'type' (character) of argument

I used the same aggregate code from you.

aggregate(formula = amount ~ group + class,

  •       data = sample_data,
  •       FUN = sum)

Any ideas?

Nevermind! I used mutate(as.numeric) and now it is working. Thanks!

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