I have a list of isometric structures, and I want to change the same part of each structure in the list, assigning one element of a vector to each of these parts. In other words, I want to achieve the following: > l <- list( list(a=1,b=2), list(a=3,b=4)) > unlist(lapply(l, "[[", "a")) [1] 1 3 > > # This will not actually work > l[[]]["a"] <- 5:6 > > unlist(lapply(l, "[[", "a")) [1] 5 6 I figure mapply is the solution to the problem, and I tried the following: > mapply( "[<-", l, "a", 5:6) But the result is not the same shape as the original. Any thoughts? Best, Magnus
On Mon, 12 Oct 2009, Magnus Torfason wrote:> I have a list of isometric structures, and I want to change the same part of > each structure in the list, assigning one element of a vector to each of > these parts. > > In other words, I want to achieve the following: > >> l <- list( list(a=1,b=2), list(a=3,b=4)) >> unlist(lapply(l, "[[", "a")) > [1] 1 3 >> >> # This will not actually work >> l[[]]["a"] <- 5:6 >> >> unlist(lapply(l, "[[", "a")) > [1] 5 6 > > I figure mapply is the solution to the problem, and I tried the following: > >> mapply( "[<-", l, "a", 5:6) > > But the result is not the same shape as the original. > > Any thoughts?See ?relist something like: relist( unlist( mapply( "[<-", l, "a", 5:6) ), l ) HTH, Chuck> > Best, > Magnus > > ______________________________________________ > 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. > >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
Hmmm, after thinking about it, better see ?mapply SIMPLIFY logical; attempt to reduce the result to a vector or matrix? Try mapply( ..., SIMPLIFY=FALSE ) HTH, Chuck On Mon, 12 Oct 2009, Magnus Torfason wrote:> I have a list of isometric structures, and I want to change the same part of > each structure in the list, assigning one element of a vector to each of > these parts. > > In other words, I want to achieve the following: > >> l <- list( list(a=1,b=2), list(a=3,b=4)) >> unlist(lapply(l, "[[", "a")) > [1] 1 3 >> >> # This will not actually work >> l[[]]["a"] <- 5:6 >> >> unlist(lapply(l, "[[", "a")) > [1] 5 6 > > I figure mapply is the solution to the problem, and I tried the following: > >> mapply( "[<-", l, "a", 5:6) > > But the result is not the same shape as the original. > > Any thoughts? > > Best, > Magnus > > ______________________________________________ > 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. > >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
Try: mapply(..., SIMPLIFY = FALSE) On Mon, Oct 12, 2009 at 5:25 PM, Magnus Torfason <zulutime.net at gmail.com> wrote:> I have a list of isometric structures, and I want to change the same part of > each structure in the list, assigning one element of a vector to each of > these parts. > > In other words, I want to achieve the following: > >> l <- list( list(a=1,b=2), list(a=3,b=4)) >> unlist(lapply(l, "[[", "a")) > [1] 1 3 >> >> # This will not actually work >> l[[]]["a"] <- 5:6 >> >> unlist(lapply(l, "[[", "a")) > [1] 5 6 > > I figure mapply is the solution to the problem, and I tried the following: > >> mapply( "[<-", l, "a", 5:6) > > But the result is not the same shape as the original. > > Any thoughts? > > Best, > Magnus > > ______________________________________________ > 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. >
Thank you so much, relist and SIMPLIFY both work. See more comments below ... On 10/12/2009 5:35 PM, Charles C. Berry wrote:> On Mon, 12 Oct 2009, Magnus Torfason wrote: >> I want to achieve the following: >> >>> l <- list( list(a=1,b=2), list(a=3,b=4)) >>> l[[]]["a"] <- 5:6>> See > ?relist > > something like: > > relist( unlist( mapply( "[<-", l, "a", 5:6) ), l )Yes, this works exactly as needed. I had tried this, but I failed to notice that you could supply the skeleton argument separately. Thanks! I had also tried mapply( "[<-", l, "a", c(5,6), SIMPLIFY=FALSE) but failed to put SIMPLIFY in all-caps, and got an error message that I did not understand. It seems I was close on both methods, but not close enough. Best, Magnus