[R] Variable passed to function not used in function in select=... in subset

Duncan Murdoch murdoch at stats.uwo.ca
Mon Nov 10 16:31:57 CET 2008


On 11/10/2008 10:18 AM, Karl Knoblick wrote:
> Hello!
> 
> I have the problem that in my function the passed variable is not used, but the variable name of the dataframe itself - difficult to explain, but an easy example:
> 
> TestFunc<-function(df, group) {
>     print(names(subset(df, select=group)))
> }
> df1<-data.frame(group="G1", visit="V1", value=0.9)
> TestFunc(df1, c("group", "visit"))
> 
> Result:
> [1] "group"
>  
> But I expected and want to have [1] "group" "visit" as result! Does anybody know how to get this result?

Don't use subset.  You can get what you want using


print(names(df[,group]))

Or alternatively, you can force group to be found in the right place in 
this way:

e <- environment()
print(names(subset(df, select=e$group)))



More information about the R-help mailing list