Karl Knoblick
2008-Nov-10 15:18 UTC
[R] Variable passed to function not used in function in select=... in subset
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
Henrique Dallazuanna
2008-Nov-10 15:28 UTC
[R] Variable passed to function not used in function in select=... in subset
Try this: TestFunc<-function(df, group) { return(names(eval(bquote(subset(df1, select = .(group)))))) } On Mon, Nov 10, 2008 at 1:18 PM, Karl Knoblick <karlknoblich@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@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Duncan Murdoch
2008-Nov-10 15:31 UTC
[R] Variable passed to function not used in function in select=... in subset
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)))
Gabor Grothendieck
2008-Nov-10 15:43 UTC
[R] Variable passed to function not used in function in select=... in subset
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. >
Gabor Grothendieck
2008-Nov-10 16:06 UTC
[R] Variable passed to function not used in function in select=... in subset
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. >> >
Wacek Kusnierczyk
2008-Nov-11 17:45 UTC
[R] Variable passed to function not used in function in select=... in subset
Gavin Simpson wrote:> > I've found several of these discussions involving Wacek's questions very > enlightening at times; once you get past the "it doesn't work as I > expect so is wrong" attitude. >just one fix: my attitude is 'it doesn't work as i imagine an average user would expect it so it's potentially confusing'. vQ -- ------------------------------------------------------------------------------- Wacek Kusnierczyk, MD PhD Email: waku at idi.ntnu.no Phone: +47 73591875, +47 72574609 Department of Computer and Information Science (IDI) Faculty of Information Technology, Mathematics and Electrical Engineering (IME) Norwegian University of Science and Technology (NTNU) Sem Saelands vei 7, 7491 Trondheim, Norway Room itv303 Bioinformatics & Gene Regulation Group Department of Cancer Research and Molecular Medicine (IKM) Faculty of Medicine (DMF) Norwegian University of Science and Technology (NTNU) Laboratory Center, Erling Skjalgsons gt. 1, 7030 Trondheim, Norway Room 231.05.060
Thomas Lumley
2008-Nov-11 19:48 UTC
[R] Variable passed to function not used in function in select=... in subset
Some of the uses of non-standard evaluation are undoubtedly a problem in R. Probably the worst is in model.frame, because it is much harder to work around. I have never used subset(,select=) and hence have never been at risk of confusion (if you don't like how it works, I suggest you do the same), but model.frame() is inside lots of things. There are two issues here that I think are worth pointing out: 1/ Some things are just not fixable any more. They can only be fixed in a new language. The people thinking about new statistical languages mostly know what the problems are, because they have been using S and/or R for many years and it's really not that hard to notice the problems. The document on non-standard evaluation demonstrates that R-core is aware of this particular problem. 2/ There are some uses of non-standard evaluation that don't seem to confuse people, and an interesting question is how to characterise them. These are what I referred to as 'macro-like functions' in the document that you have already been referred to. For example, subset(,subset=) and with() don't seem to be as confusing or to cause problems for programmers in the same way. There is an empirical question as to what these relatively non-problematic constructs are, and a theoretical question as to why they are different. In particular, with() not only has non-standard evaluation, it is quite similar to the notoriously confusing attach(). -thomas On Tue, 11 Nov 2008, Wacek Kusnierczyk wrote:> Berwin A Turlach wrote: >> On Tue, 11 Nov 2008 09:27:41 +0100 >> Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote: >> >> >>> but then it might be worth asking whether carrying on with misdesign >>> for backward compatibility outbalances guaranteed crashes in future >>> users' programs, [...] >>> >> >> Why is it worth asking this if nobody else asks it? > > > i guess most of the people who do ask questions here care little about r > itself, they just want it to solve a problem, even if it involves > hacking the language. > > those outside the r team who care about language design have probably > left the list long ago, if only they were subscribed. the fact that > it's only me asking is no statistics. i do talk to people, and know > many who'd ask, but they just don't care, because they have already > trashed r. instead of discouraging me, make use of that i care to ask. > > vQ > > ______________________________________________ > 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. >Thomas Lumley Assoc. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle