I know there is an easy way to do this…but, I can’t figure it out.
I have a dataframe in my R script that looks something like this:
A B C
1.2 4 8
2.3 4 9
2.3 6 0
1.2 3 3
3.4 2 1
1.2 5 1
Note that A, B, and C are column names. And I’m trying to get variables like this:
sum1 <- [the sum of all B values such that A is 1.2]
num1 <- [the number of times A is 1.2]
Any easy way to do this?
I basically want to end up with a data frame that looks like this:
A num totalB
1.2 3 12
etc etc etc
Where “num” is the number of times that particular A value appeared, and “totalB” is the sum of the B values given the A value.
I’d use
aggregateto get the two aggregates and thenmergethem into a single data frame: