Ed G
Ed G

Reputation: 822

Passing a value from a function into ddply

I've got ddply constructing a data.frame along these lines:

out <- ddply(data, .(names), varA = sum(value > 10))

That works fine, so I've tried to place it into a function

func <- function(val.in) {
    out <- ddply(data, .(names), varA = sum(value > val.in))
}

func(10)

This doesn't work - it looks like ddply can't find 'val.in'

Error in eval(expr, envir, enclos) : object 'val.in' not found

Anyone know why?

If not enough background, let me know and I'll update.

Upvotes: 2

Views: 1611

Answers (2)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193517

I've tried to recreate your problem using some sample data from the examples under ddply.

First, some sample data:

dfx <- data.frame(
  group = c(rep('A', 8), rep('B', 15), rep('C', 6)),
  sex = sample(c("M", "F"), size = 29, replace = TRUE),
  age = runif(n = 29, min = 18, max = 54)
)

head(dfx)
#   group sex      age
# 1     A   F 53.08787
# 2     A   M 30.47225
# 3     A   F 26.78341
# 4     A   F 26.46841
# 5     A   F 34.65360
# 6     A   M 21.26691

Here's what you might try that would work (I assume you meant to use summarize in your question).

library(plyr)
ddply(dfx, .(group, sex), summarize, varA = sum(age > 25))
#   group sex varA
# 1     A   F    5
# 2     A   M    1
# 3     B   F    6
# 4     B   M    4
# 5     C   F    3
# 6     C   M    2

We might then try to use it in a function as follows:

func <- function(val.in) {
  out <- ddply(dfx, .(group, sex), summarize, varA = sum(age > val.in))
  out
}

func(25)
# Error in eval(expr, envir, enclos) : object 'val.in' not found

^^ There's your error ^^


The most straightforward solution is to use here (which helps ddply figure out where to look for things):

func <- function(val.in) {
  out <- ddply(dfx, .(group, sex), here(summarize), varA = sum(age > val.in))
  out
}

func(25)
#   group sex varA
# 1     A   F    5
# 2     A   M    1
# 3     B   F    6
# 4     B   M    4
# 5     C   F    3
# 6     C   M    2

Update

This doesn't seem to be a problem in "dplyr" as far as I can tell:

library(dplyr)
myFun <- function(val.in) {
  dfx %>% group_by(group, sex) %>% summarise(varA = sum(age > val.in))
}
myFun(10)
# Source: local data frame [6 x 3]
# Groups: group
#
#   group sex varA
# 1     A   F    5
# 2     A   M    3
# 3     B   F    7
# 4     B   M    8
# 5     C   F    2
# 6     C   M    4

Upvotes: 13

Ari B. Friedman
Ari B. Friedman

Reputation: 72731

Seems like you want to write an anonymous function and pass in the second argument:

func<-function(val.in){
    ddply(data, .(names), function(value,val.in) data.frame(varA=sum(value>val.in)), val.in)
}

Upvotes: 0

Related Questions