On Nov 19, 2012, at 10:46 AM, William Dunlap wrote:
If you have a list and want to add a new (or replace a)
named component use
myList[[compName]] <- compValue
as in
myList <- list()
compName <- "Incr"
compValue <- function(x) x + 1
myList[[compName]] <- compValue
If you want to make a new list-with-names from scratch try
structure(list(1, "cat", function(x)x+1),
names=c("One","Pet","Increment"))
(structure() is a general way to make an object and add attributes to
it in one statement.)
I'm guessing that Sam wanted to see:
myList <- list()
myList[[ paste0("fo", "o") ]] <- 10
> myList
$foo
[1] 10
Or:
structure(list(10), names=paste0("fo", "o") )
$foo
[1] 10
(At least that's my guess from the context of the question.)
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-----Original Message-----
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org
] On Behalf
Of Sam Steingold
Sent: Monday, November 19, 2012 10:07 AM
To: r-help@r-project.org
Subject: [R] generated list element names
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@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@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.
David Winsemius, MD
Alameda, CA, USA
______________________________________________
R-help@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.