Dan Abner
2013-Oct-14 15:09 UTC
[R] Passing multiple object names to a user-defined R fn XXXX
Hi all, I am attempting to write an R fn that will accept multiple (but varying on how many) objects (usually data frames) as inputs and return summary output. What is the best way to pass the object names to the fn (I have thought of 2 options below) AND how do I then use the names inside the fn to reference the actual object (I assume that I would need something like get(x[1]) for example to have "d1" resolve the the object d1. Correct? The 1st option I thought of was to pass the object names as a character vector in the call to the fn: set.matrix<-df.set(c("d1","d2","d3","d4","d5","d6","d7","d8"),by="ID") 2nd option: Is something like this possible: set.matrix<-df.set(c(d1,d2,d3,d4,d5,d6,d7,d8),by="ID") [[alternative HTML version deleted]]
PIKAL Petr
2013-Oct-14 15:31 UTC
[R] Passing multiple object names to a user-defined R fn XXXX
Hi I will not answer your question directly but instead I have some recommendation for you. If you can put these data frames to one list object when you creates them, you can save yourself a lot of troubles. some.list<-vector("list", n) for (i in 1:n) { some.list[[i]] <- some extensive computation or reading files } then you can use lapply/sapply/for cycle to make any computation directly on list. I use R for more than 10 years and I do not remember any situation when I would need to create multiple objects in "n1":"nx" naming manner. Regards Petr> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Dan Abner > Sent: Monday, October 14, 2013 5:10 PM > To: r-help at r-project.org > Subject: [R] Passing multiple object names to a user-defined R fn XXXX > > Hi all, > > I am attempting to write an R fn that will accept multiple (but varying > on how many) objects (usually data frames) as inputs and return summary > output. > > What is the best way to pass the object names to the fn (I have thought > of > 2 options below) AND how do I then use the names inside the fn to > reference the actual object (I assume that I would need something like > get(x[1]) for example to have "d1" resolve the the object d1. Correct? > > The 1st option I thought of was to pass the object names as a character > vector in the call to the fn: > > set.matrix<-df.set(c("d1","d2","d3","d4","d5","d6","d7","d8"),by="ID") > 2nd option: Is something like this possible: > > set.matrix<-df.set(c(d1,d2,d3,d4,d5,d6,d7,d8),by="ID") > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
Rui Barradas
2013-Oct-14 15:43 UTC
[R] Passing multiple object names to a user-defined R fn XXXX
Hello, You should put all those data frames in a list and then use the list (just one object). But if you have several objects in your workspace and need to write a function that accepts a varying number of arguments, use the dots argument: df.set <- function(..., by){ dots <- list(...) # all those objects in a list for(d in dots) print(summary(d)) invisible(NULL) } d1 <- data.frame(x = 1:4, y = 5:8) d2 <- data.frame(a = 1:10, b = 11:20) df.set(d1, d2) Note that I don't use c(d1, d2), it will give something completely different. c() has unwanted side effects. Try it. Hope this helps, Rui Barradas Em 14-10-2013 16:09, Dan Abner escreveu:> Hi all, > > I am attempting to write an R fn that will accept multiple (but varying on > how many) objects (usually data frames) as inputs and return summary output. > > What is the best way to pass the object names to the fn (I have thought of > 2 options below) AND how do I then use the names inside the fn to reference > the actual object (I assume that I would need something like get(x[1]) for > example to have "d1" resolve the the object d1. Correct? > > The 1st option I thought of was to pass the object names as a character > vector in the call to the fn: > > set.matrix<-df.set(c("d1","d2","d3","d4","d5","d6","d7","d8"),by="ID") > 2nd option: Is something like this possible: > > set.matrix<-df.set(c(d1,d2,d3,d4,d5,d6,d7,d8),by="ID") > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >