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