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

Gabor Grothendieck ggrothendieck at gmail.com
Mon Nov 10 17:06:34 CET 2008


Forgot the name part.  Try:

TestFunc2 <- function(DF, group) names(DF[group])
TestFunc3 <- function(...) names(subset(..., subset = TRUE))
TestFunc4 <- function(...) eval.parent(names(subset(..., subset = TRUE)))

# e.g.
df1 <- data.frame(group = "G1", visit = "V1", value = 0.9)
TestFunc2(df1, c("group", "visit"))
TestFunc3(df1, c("group", "visit"))
TestFunc4(df1, c("group", "visit"))
TestFunc4(df1, c(group, visit)) # this works too

On Mon, Nov 10, 2008 at 10:43 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> Here are a few things to try:
>
> TestFunc1 <- get("[")
>
> TestFunc2 <- function(DF, group) DF[group]
>
> TestFunc3 <- function(...) subset(..., subset = TRUE)
>
>
>
> On Mon, Nov 10, 2008 at 10:18 AM, Karl Knoblick <karlknoblich at yahoo.de> 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?
>>
>> Thanks!
>> Karl
>>
>>
>>
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>



More information about the R-help mailing list