[R] passing a vector of variable names to the ... pairlist function argument

Uwe Ligges ligges at statistik.tu-dortmund.de
Thu Apr 21 17:30:47 CEST 2011



On 21.04.2011 17:14, Jessica Myers wrote:
> Thanks - your last suggestion does seem to work with the ridge function,
> but the names of the objects get lost in the process. Is there a way to
> keep the object names with get?


Oh, come on, you can do that yourself:

L <- lapply(xs, get)
names(L) <- xs
do.call("cbind", L)


Uwe Ligges

> Thanks,
> Jessica Myers
>
> On Apr 21, 2011, at 11:00 AM, Uwe Ligges wrote:
>
>>
>>
>> On 21.04.2011 16:03, Jessica Myers wrote:
>>> Hi,
>>>
>>> I have a character vector that contains the names of several objects
>>> that I would like to pass to a function (specifically, the ridge
>>> function in the survival package, but cbind is a similar example). I've
>>> been struggling with how to do this so that the object values get
>>> interpreted by the function, rather than the object names.
>>>
>>> For example,
>>> x1 <- 1:4
>>> x2 <- 2:5
>>> x3 <- 3:6
>>> xs <- c("x1", "x2", "x3")
>>>
>>> If I wanted to cbind(x1, x2, x3) without typing this out, how would I do
>>> it?
>>
>>
>> For the cbind case, I'd omit cbind and ask R to:
>>
>> sapply(xs, get)
>>
>> or
>>
>> apply(matrix(xs), 1, get)
>>
>> or with cbind():
>>
>> do.call("cbind", lapply(xs, get))
>>
>> the latter can be generalized for other function calls, of course.
>>
>> Best,
>> Uwe Ligges
>>
>>
>>
>>
>>> Thanks very much!
>>>
>>> Jessica Myers
>>> Instructor in Medicine
>>> Division of Pharmacoepidemiology
>>> Brigham and Women's Hospital
>>>
>>>
>>>
>>> The information in this e-mail is intended only for the ...{{dropped:7}}
>>>
>>> ______________________________________________
>>> 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.
>
>
>
> The information in this e-mail is intended only for th...{{dropped:13}}



More information about the R-help mailing list