Using aggregate with variable names for column names
I have the following dataframe:
a <- c(1,1,4)
b <- c(1,0,2)
c <- data.frame(a=a,b=b)
str(c)
# a b
#1 1 1
#2 1 0
#3 4 2
I would like to aggregate a data frame c like this:
aggregate(b~a,FUN=mean,data=c)
# a b
#1 1 0.5
#2 4 2.0
However my main problem is that I will be using a variable for the column name
So:
d <- 'a'
If I try to compile this d variable that contains the column name, I obviously get the error:
aggregate(b~d,FUN=mean,data=c)
#Error in model.frame.default(formula = b ~ d, data = c) : variable lengths differ (found for 'd')
This works, but then I get silly column names. I would like to avoid the extra step of renaming columns:
aggregate(c[,'b']~c[,d],FUN=mean,data=c)
# c[, d] c[, "b"]
#1 1 0.5
#2 4 2.0
How do I fill in and also get the correct column names on the first try? (There may be no way to do this)
+3
source to share