I have a dataframe whose column names will change everytime it is generated, so I'd like to pass the column name as a variable. Let's say this is a simplified version of my dataframe:
mydf<- data.frame(colors=c('Blue','Red','Green'), weight1=c(1:6),weight2=c(10:15))
If the column name were not an issue, the following code does what I want:
x<-ddply(mydf,'colors', summarize, sum(weight1))
colors sum(weight1)
1 Blue 5
2 Green 9
3 Red 7
But if try to pass the column weight1
as a variable, it no longer sums it by group, but returns a bulk sum instead. Here are a couple of things I've tried:
ddply(mydf,'colors', summarize, sum(mydf[2]))
colors sum(mydf[2])
1 Blue 21
2 Green 21
3 Red 21
mycol <- colnames(mydf)[2]
ddply(Cars,'model', summarize, sum(get(mycol)))
Error: object 'weight1' not found
ddply(mydf,'colors', summarize, sum(eval(parse(text = mycol))))
Error: object 'weight1' not found
ddply(mydf,'colors', summarize, do.call('sum', mydf[2]))
colors do.call("sum", mydf[2])
1 Blue 21
2 Green 21
3 Red 21
Any suggestions?