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

Bert Gunter gunter.berton at gene.com
Mon Oct 14 18:01:13 CEST 2013


Sorry about the previous partial message. My email screwed up. Here's
the complete message:

df.set <- function(...){
        dots <- list(...)  # all those objects in a list
        lapply(dots,summary)
}

Cheers,
Bert

On Mon, Oct 14, 2013 at 8:59 AM, Bert Gunter <bgunter at gene.com> wrote:
> A minor quibble.
>
> I would argue that R best practice would be to keep the summarization
> and printing separate; viz
>
> df.set <- function(...){
>         dots <- list(...)  # all those objects in a list
>
> }
>
> On Mon, Oct 14, 2013 at 8:43 AM, Rui Barradas <ruipbarradas at sapo.pt> wrote:
>> 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.
>>>
>>
>> ______________________________________________
>> 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.
>
>
>
> --
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
> (650) 467-7374



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

(650) 467-7374



More information about the R-help mailing list