I am new to R, and have discovered the aggregate
command. I don't know if there is a way to apply it with two variables instead of one, however.
I have a dataframe, approval_agg
which has four columns of month_year
which is the month, subgroup
which is a demographic, and approve_estimate
and disapprove_estimate
which are approval and disapproval ratings, respectively.
I would like to get the average ratings for each month and subgroup. Some example data I posted below:
month_year subgroup approve_estimate disapprove_estimate
2020-11-01 Voters 53 47
2020-11-01 All polls 56 44
2020-11-01 Adults 54 46
2020-11-01 Voters 54 46
2020-11-01 All polls 53 47
2020-11-01 Adults 49 51
2020-10-01 Voters 57 43
2020-10-01 All polls 56 44
2020-10-01 Adults 60 40
2020-10-01 Voters 51 49
2020-10-01 All polls 57 43
2020-10-01 Adults 53 47
which I would like to get:
2020-11-01 Voters 53.5 46.5
2020-11-01 All polls 54.5 45.5
2020-11-01 Adults 51.5 48.5
2020-10-01 Voters 56 44
2020-10-01 All polls 56.5 43.5
2020-10-01 Adults 56.5 43.5
I have my aggregate column for one column as
aggregate(. ~ month_year, df, mean)
, but I get NA
values. Is there a way I can use aggregate
or anything to get these mean values?