[R] generated list element names

jim holtman jholtman at gmail.com
Mon Nov 19 19:37:48 CET 2012


I missed the last snipet; just saw the first.  So you have your
solution.  If you want a function, try:

> f.newList <- function(x,name){.x <- list(x);names(.x) <- name;.x}
> f.newList(10, paste('f', 'oo', sep = ''))
$foo
[1] 10


On Mon, Nov 19, 2012 at 1:32 PM, Sam Steingold <sds at gnu.org> wrote:
>> * jim holtman <wubygzna at tznvy.pbz> [2012-11-19 13:14:05 -0500]:
>>
>> How about this (if you don't like writing two lines, encapsulate it in
>> a function):
>>
>>> x <- list(10)
>>> names(x) <- paste('f', 'oo', sep = '')
>>> str(x)
>> List of 1
>>  $ foo: num 10
>>>
>
> I am sorry, how is this different from my second snippet (except that
> you use "x" and I use "z" and you use single quotes in paste and I use
> double quotes)?
>
>>
>> On Mon, Nov 19, 2012 at 1:07 PM, Sam Steingold <sds at gnu.org> wrote:
>>> How can I create lists with element names created on the fly?
>>>
>>> --8<---------------cut here---------------start------------->8---
>>>> list (foo = 10)
>>> $foo
>>> [1] 10
>>>
>>>> list ("foo" = 10)
>>> $foo
>>> [1] 10
>>>
>>>> list (paste("f","oo",sep="") = 10)
>>> Error: unexpected '=' in "list (paste("f","oo",sep="") ="
>>> --8<---------------cut here---------------end--------------->8---
>>>
>>> I understand that tags in list() are not evaluated, but is there a more
>>> elegant way than
>>>
>>> --8<---------------cut here---------------start------------->8---
>>>> z <- list(10)
>>>> names(z) <- paste("f","oo",sep="")
>>>> z
>>> $foo
>>> [1] 10
>>> --8<---------------cut here---------------end--------------->8---
>>>
>>> thanks!
>>>
>>> --
>>> Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
>>> http://www.childpsy.net/ http://www.memritv.org
>>> http://thereligionofpeace.com http://truepeace.org
>>> Unix roulette: `dd if=/dev/urandom of=/dev/kmem bs=1 count=1 seek=$RANDOM`
>>>
>>> ______________________________________________
>>> 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.
>
> --
> Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
> http://www.childpsy.net/ http://memri.org http://truepeace.org http://ffii.org
> http://think-israel.org http://jihadwatch.org http://palestinefacts.org
> The only time you have too much fuel is when you're on fire.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.




More information about the R-help mailing list