19

I'd like to be able to send in a column name to a call that I am making to ddply. An example ddply call:

ddply(myData, .(MyGrouping), summarise, count=sum(myColumnName))

If I have ddply wrapped within another function is it possible to wrap this so that I can pass in an arbitrary value as myColumnName to the calling function?

5
  • Do you have a reproducible example (with data, e.g. using iris)? Apr 16, 2012 at 19:25
  • @static_rtti are you specifically looking for a plyr answer? This question is very old and there are way more advanced packages these days. Apr 15, 2015 at 11:57
  • Well, I use plyr (and don't know of more advanced packages), but if you can provide an answer to the same problem with a different package, I would find that interesting too. Apr 15, 2015 at 11:59
  • I would have thought that here could have helped, but it doesn't seem to make things any easier.... Apr 15, 2015 at 14:57
  • @static_rtti The answer is that summarise is not designed for this. Similar to other convenience functions you should use alternatives for some cases where you work programmatically.
    – Roland
    Apr 17, 2015 at 15:44

3 Answers 3

11

There has got to be a better way. And I couldn't figure out how to make it work with summarise.

my.fun <- function(df, count.column) { 
  ddply(df, .(x), function(d) sum(d[[count.column]]))
}

dat <- data.frame(x=letters[1:2], y=1:10)

> my.fun(dat, 'y')
  x V1
1 a 25
2 b 30
> 
3
  • You could pass the count.column directly to the function in ddply as in: ddply(df, .(x), function(d) sum(d[[count.column]])) meaning less code and avoiding the eval. Apr 16, 2012 at 17:38
  • @TylerRinker yeah, that eval was a holdover from when I was trying to make it work with summarise. edited.
    – Justin
    Apr 16, 2012 at 17:42
  • I attempted this too (but since don't really use plyr much any more) I also couldn't figure out how to make it work with summarise. Apr 16, 2012 at 17:44
5
+100

As what @David Arenburg said, this question is pretty old. Today, either data.table or dplyr package can give you the same result with a much faster speed.

Here is the data.table version of the answer.

library(data.table)
my.fun <- function(myData, MyGrouping, myColumnName) { 
  setDT(myData)[, lapply(.SD, sum), by=MyGrouping, .SDcols=myColumnName]
}
0

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
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.