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: