How do I append an R-object to a list? I want to start with an empty list, and append R-objects one by one. Does this start with a command like mylist <- NULL ?? I have read a few answers on R-help to questions like this, but they all seem to be well off the point. Sometimes it's assumed that the list is a vector---not my case. One answer I read said that the object appended must be a list. This doesn't make sense to me. I don't want a list of lists. I want a list of R-objects. I tried using c(with.stuff.inside.the.brackets). This seemed to unwrap my structures, storing the individual components rather than the R-objects themselves. I tried looking in "Intro to R" but could find nothing relevant. Thanks for any help. David -- View this message in context: http://n4.nabble.com/appending-an-R-object-to-a-list-tp1753544p1753544.html Sent from the R help mailing list archive at Nabble.com.
On Tue, Apr 6, 2010 at 10:28 PM, David.Epstein <David.Epstein at warwick.ac.uk> wrote:> > How do I append an R-object to a list? > I want to start with an empty list, and append R-objects one by one. > Does this start with a command like > mylist <- NULL > ?? > > I have read a few answers on R-help to questions like this, but they all > seem to be well off the point. Sometimes it's assumed that the list is a > vector---not my case. > One answer I read said that the object appended must be a list. This doesn't > make sense to me. I don't want a list of lists. I want a list of R-objects.Start with an empty list() and just assign to the next element: > z=list() > z[[1]]=c(1,2,3) > z[[2]]=c(9,5,4) > z [[1]] [1] 1 2 3 [[2]] [1] 9 5 4 - that's a "list of R objects", in this case the R objects are simple vectors, but they could be any R objects - fitted models, spatial point patterns etc etc. Is that what you want? You can also just add elements by name: z = list() z$foo = c(1,2,3) z$fnord = "hello" z[[1]] and z[[2]] are also valid here. Barry
Thanks. A nice simple answer and exactly what I want. David -- View this message in context: http://n4.nabble.com/appending-an-R-object-to-a-list-tp1753544p1753665.html Sent from the R help mailing list archive at Nabble.com.
On 06.04.2010 23:28, David.Epstein wrote:> > How do I append an R-object to a list? > I want to start with an empty list, and append R-objects one by one. > Does this start with a command like > mylist<- NULL > ?? > > I have read a few answers on R-help to questions like this, but they all > seem to be well off the point. Sometimes it's assumed that the list is a > vector---not my case. > One answer I read said that the object appended must be a list. This doesn't > make sense to me. I don't want a list of lists. I want a list of R-objects. > > I tried using c(with.stuff.inside.the.brackets). This seemed to unwrap my > structures, storing the individual components rather than the R-objects > themselves. > > I tried looking in "Intro to R" but could find nothing relevant. > > Thanks for any help. > David > >Well lists are vectors of mode list. If you have a list L and want to append an element l, you can simply add it by: c(L, list(l)) One element of a List L extracted by vector indexing (single brackets): L[1] will show you got a list of length one back (since L is a vector of mode list). You may want to play around and find that things are even simpler than you thought they are. Uwe Ligges