Skip to content

Instantly share code, notes, and snippets.

@suncoolsu
Created December 6, 2010 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suncoolsu/730069 to your computer and use it in GitHub Desktop.
Save suncoolsu/730069 to your computer and use it in GitHub Desktop.
Using as.name in ddply .(variable) results in an error "object 'grp' not found"
## Doesn't work
# grp -- name of a column of the the data.frame df
# function call is -- getMinMax1( df1 , grp = "g10")
getMinMax1 <-function(df, grp){
dfret <- ddply( df , .(as.name(grp)), ## I am using as.name(grp), source of error
function(x){
minmax <- c(mix(x[ , 3]), max(x[ ,3]))
return(minmax)
}
)
return(dfret)
}
## Works fine
# grp -- name of a column of the the data.frame df
# function call is -- getMinMax2( df1 , grp = "g10")
getMinMax2 <-function(df, grp){
dfret <- ddply( df , grp, ## using the quoted variable name passed to grp when the fun is called
function(x){
minmax <- c(min(x[ , 3]), max(x[ ,3]))
return(minmax)
}
)
return(dfret)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment