The following will solve the immediate problem:
lapply(x, FUN=function(x){
x[[3]] <- matrix(rnorm(100,10, 2), 10,10)
return(x)
})
A more generalized version (if you actually wanted control over what was being
placed into x[[k]][[3]]) would be:
(I've simplified your x but kept the same structure)
x <- list(list(1, list("a"),list()),
list(2, list("b"),list()),
list(3, list("c"),list()))
(for your problem, replace v1, etc. with your randome matrices or
anything else)
value <- list("v1", "v2", "v3")
y <- lapply(1:length(x), FUN=function(k, x, value){
x[[k]][[3]] <- value[[k]]
return(x[[k]])
}, x=x, value=value)
-------------------------
William A. Stewart
Manager, Operational Risk
RBC Financial Group
william.stewart@rbc.com
phone: 416-974-3019
-----Original Message-----
From: s-news-owner@lists.biostat.wustl.edu
[mailto:s-news-owner@lists.biostat.wustl.edu]On Behalf Of Dave Evens
Sent: Wednesday, August 17, 2005 9:39 AM
To: s-news@lists.biostat.wustl.edu
Subject: [S] List question
Dear all,
My query involves a list. For example, suppose I have
a list x
x <- list(list(rpois(100,2), list(LETTERS[1:20]),
list()),
list(rpois(150,2), list(LETTERS[1:12]),
list()),
list(rpois(200,2), list(LETTERS[1:19]),
list()))
and I want to replace the empty list in each sub-list
of x, this is easy using the for-loop
for(k in 1:length(x)) x[[k]][[3]] <- matrix(rnorm(100,
10, 2), 10,10)
but how do I the equivalent of this using the lapply
command?
This is the syntax of the lapply command
lapply(1:length(x), function(k) matrix(rnorm(100, 10,
2), 10,10))
but how do I get it in x[[k]][[3]]?
I've tried using the assign command but it doesn't
appear to work.
Thanks for any help in advance.
Dave
____________________________________________________
Start your day with Yahoo! - make it your home page
http://www.yahoo.com/r/hs
--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu. To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message: unsubscribe s-news
__________________________________________________________________________________________________________________________________
This e-mail may be privileged and/or confidential, and the sender does not
waive any related rights and obligations.
Any distribution, use or copying of this e-mail or the information it contains
by other than an intended recipient is unauthorized.
If you received this e-mail in error, please advise me (by return e-mail or
otherwise) immediately.
Ce courrier électronique est confidentiel et protégé. L'expéditeur ne renonce
pas aux droits et obligations qui s'y rapportent.
Toute diffusion, utilisation ou copie de ce message ou des renseignements qu'il
contient par une personne autre que le (les) destinataire(s) désigné(s) est
interdite.
Si vous recevez ce courrier électronique par erreur, veuillez m'en aviser
immédiatement, par retour de courrier électronique ou par un autre moyen.
Stewart, William.vcf
Description: Stewart, William.vcf
|