I want to use aggregate function on a date frame but sum one column and take average of another column.
Here is an example data frame
Manager Category Amount SqFt
Joe Rent 150 500
Alice Rent 250 700
Joe Utilities 50 500
Alice Utilities 75 700
I cannot do something like below. Is there an easy way to do it ?
Avg_CPSF=aggregate(cbind(Amount,SqFt)~Manager,data=aaa,FUN=c(sum,mean)
Eventually I need
Manager Amount SqFT
Joe 200 500
Alice 325 700
so that I can calculate Cost per Square Foot by doing Amount/SqFT
There are several ways to do this. Here are some that I like (all assuming we’re starting with a
data.framenamed “mydf”):Using
aveanduniqueUsing
data.table:Using “sqldf”:
Using
aggregateandmerge: