I guess I found a way that works with summarise. I'm not sure if I understand why, since I'm no expert in dealing with environments in R, but here's the solution:
> library(plyr)
>
>
>
> ###########################
> # Creating test DataFrame #
> ###########################
>
> x <- 1:15
>
> set.seed(1)
> y <- letters[1:3][sample(1:3, 15, replace = T)]
>
> df <- data.frame(x, y)
>
> ### check df
> df
x y
1 1 a
2 2 b
3 3 b
4 4 c
5 5 a
6 6 c
7 7 c
8 8 b
9 9 b
10 10 a
11 11 a
12 12 a
13 13 c
14 14 b
15 15 c
>
>
> #####################
> # auxiliar function #
> #####################
> evalString <- function(s) {
+ eval(parse(text = s), parent.frame())
+ }
>
>
> ### columnName input
> columnName <- 'x'
>
> ### call with columnName as input
> xMeans <- ddply(df,
+ 'y',
+ summarise,
+ mean = mean(evalString(columnName)))
>
>
> ### regular call to ddply
> xMeans2 <- ddply(df,
+ 'y',
+ summarise,
+ mean = mean(x))
>
>
> ### Compare Results
> xMeans
y mean
1 a 7.8
2 b 7.2
3 c 9.0
> xMeans2
y mean
1 a 7.8
2 b 7.2
3 c 9.0
>
EDIT: You can use the get
function from the base package, as suggested here: ddply: how do I pass column names as parameters?
> xMeans3 <- ddply(df,
+ 'y',
+ summarise,
+ mean = mean(get(columnName)))
>
> xMeans3
y mean
1 a 7.8
2 b 7.2
3 c 9.0
plyr
answer? This question is very old and there are way more advanced packages these days.here
could have helped, but it doesn't seem to make things any easier....summarise
is not designed for this. Similar to other convenience functions you should use alternatives for some cases where you work programmatically.