Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using aggregate with variable names for column names

I have the following data frame:

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 the data frame c in the following way:

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 name of the column

So:

d <- 'a'

If I try to aggregate using this variable d that contains the name of the column, I will obviously get an 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 I then 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 to I aggregate and also get the right column names the first try? (Maybe there is no way to do this)

like image 588
Michal Avatar asked Nov 26 '25 20:11

Michal


1 Answers

You could try

aggregate(c['b'], c[d], FUN=mean)
#   a   b
# 1 1 0.5
# 2 4 2.0

Another option if you are using the formula method would be to use setNames

 setNames(aggregate(b~get(d), FUN=mean, data=c), colnames(c))
 #  a   b
 #1 1 0.5
 #2 4 2.0
like image 180
akrun Avatar answered Nov 28 '25 15:11

akrun