Created
December 6, 2010 09:40
-
-
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"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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