[R] Passing multiple object names to a user-defined R fn XXXX

Rui Barradas ruipbarradas at sapo.pt
Mon Oct 14 17:43:10 CEST 2013


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.
>



More information about the R-help mailing list