Hello all, Maybe I'm being thick, but I was trying to figure out a simple way to create a list with the same dimension of another list, but populated with NA values. masterlist = list( aa=list( a=matrix(rnorm(100),10,10), b=matrix(rnorm(130),13,10), c=matrix(rnorm(140),14,10)), bb=list( a=matrix(rnorm(100),10,10), b=matrix(rnorm(110),11,10), c=matrix(rnorm(120),12,10)), cc=list( a=matrix(rnorm(100),10,10), b=matrix(rnorm(100),10,10), c=matrix(rnorm(100),10,10)), dd=matrix(rnorm(225),15,15)) str(masterlist) ## Yay! A new list that's just like the master list! newlist = masterlist ## BOO! Can't just set all the values to NA! newlist = masterlist * NA ## This works... but requires knowledge of the structure newlist = masterlist newlist[['aa']] = lapply(masterlist[['aa']], function(x) x * NA) newlist[['bb']] = lapply(masterlist[['bb']], function(x) x * NA) newlist[['cc']] = lapply(masterlist[['cc']], function(x) x * NA) newlist[['dd']] = masterlist[['dd']] * NA str(newlist) At this point I thought a recursive function would be a good idea, but I wasn't sure how to keep the nice list names. I feel sure that this must be something that others have already done. Thank you!!! PS: I found an email in the archive about it, but that email un-helpfully instructed the user to LEARN HOW TO USE EMAIL. Not sure how to use email to initialize a list. so if someone could explain to me how to use emails to initialize / populate a list, that would be cool also. Also, I did type ?list and ?lapply, and I did read the help files. In fact, I learned about the very helpful vector("list", length) command yesterday! Pretty cool! I'm not opposed to looking at the help, but please provide some hint about what direction to take with the help. Even a small hint will do!! If you want to yell at me about reading the help or whatever, that's fine. But please do so offline so that the archives will be more valuable for future users. [[alternative HTML version deleted]]
Erik Iverson
2011-Feb-17 22:11 UTC
[R] Populate a list / recursively set list values to NA
Gene, ?rapply is a recursive version of ?lapply, and should work. rapply(masterlist, function(x) x*NA, how = "replace") --Erik Gene Leynes wrote:> Hello all, > > Maybe I'm being thick, but I was trying to figure out a simple way to create > a list with the same dimension of another list, but populated with NA > values. > > masterlist = list( > aa=list( > a=matrix(rnorm(100),10,10), > b=matrix(rnorm(130),13,10), > c=matrix(rnorm(140),14,10)), > bb=list( > a=matrix(rnorm(100),10,10), > b=matrix(rnorm(110),11,10), > c=matrix(rnorm(120),12,10)), > cc=list( > a=matrix(rnorm(100),10,10), > b=matrix(rnorm(100),10,10), > c=matrix(rnorm(100),10,10)), > dd=matrix(rnorm(225),15,15)) > str(masterlist) > ## Yay! A new list that's just like the master list! > newlist = masterlist > ## BOO! Can't just set all the values to NA! > newlist = masterlist * NA > > ## This works... but requires knowledge of the structure > newlist = masterlist > newlist[['aa']] = lapply(masterlist[['aa']], function(x) x * NA) > newlist[['bb']] = lapply(masterlist[['bb']], function(x) x * NA) > newlist[['cc']] = lapply(masterlist[['cc']], function(x) x * NA) > newlist[['dd']] = masterlist[['dd']] * NA > str(newlist) > > At this point I thought a recursive function would be a good idea, but I > wasn't sure how to keep the nice list names. > > I feel sure that this must be something that others have already done. > > Thank you!!! > > PS: > I found an email in the archive about it, but that email un-helpfully > instructed the user to LEARN HOW TO USE EMAIL. Not sure how to use email to > initialize a list. so if someone could explain to me how to use emails to > initialize / populate a list, that would be cool also. > Also, I did type ?list and ?lapply, and I did read the help files. In fact, > I learned about the very helpful vector("list", length) command yesterday! > Pretty cool! > I'm not opposed to looking at the help, but please provide some hint about > what direction to take with the help. Even a small hint will do!! > > If you want to yell at me about reading the help or whatever, that's fine. > But please do so offline so that the archives will be more valuable for > future users. > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.