[R] define a list with names as variables

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Fri Aug 4 15:12:57 CEST 2017


You can wrap the list-creating function call (e.g. lapply) in a call to ?setNames, or you can use the ?map function from the purrr package. 
-- 
Sent from my phone. Please excuse my brevity.

On August 4, 2017 3:14:44 AM PDT, Ulrik Stervbo <ulrik.stervbo at gmail.com> wrote:
>Hi Giovani,
>
>I would create an unnamed list and set the names after.
>
>Best,
>Ulrik
>
>On Fri, 4 Aug 2017 at 12:08 Giovanni Gherdovich
><g.gherdovich at gmail.com>
>wrote:
>
>> Hello,
>>
>> I'm having troubles defining a list where names are variables (of
>type
>> character). Like this, which gives "foo" instead of "world" (the way
>I
>> meant it is that "world" is the value of the variable foo). Any hint?
>>
>> > f <- function(foo, bar) { list(foo = bar) }
>> > x <- f("hello", "world")
>> > names(x)
>> [1] "foo"
>>
>>
>> Thanks,
>> Giovanni
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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