Note that c(list(1,2,3), list(4, 5,6), list(7,8,9, 10)) is identical to list(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
You want either list(list(1,2,3), list(4,5,6), list(7,8,9,10)) # list of 3 lists of numeric scalars or list(c(1,2,3), c(4,5,6), c(7,8,9,10)) # list of 3 numeric vectors In any case, you need to wrap it in I() when passing it to data.frame so it doesn't get converted to columns of a data.frame. E.g., > df <- data.frame(name = c("a","b","c"), + type = c(1, 2, 3), + rtn = I( list(c(1,2,3), c(4,5,6), c(7,8,9,10)) )) > df[2, "rtn"] [[1]] [1] 4 5 6 > df[[2, "rtn"]] [1] 4 5 6 > df name type rtn 1 a 1 1, 2, 3 2 b 2 4, 5, 6 3 c 3 7, 8, 9, 10 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 Kevin Zembower > Sent: Friday, March 08, 2013 4:49 PM > To: r-help@r-project.org > Subject: [R] data.frame with variable-length list > > Hello, > > I'm trying to create a data frame with three columns, one of which is a > variable-length list. I tried: > > df <- data.frame(name = c("a", "b", "c"), > type=c(1, 2, 3), > rtn = c(list(1,2,3), list(4, 5,6), list(7,8,9, 10) > ) > ) > > This would be useful, for example, if the 'rtn' is a variable number of > observations. > > That gave me: > > df > name type rtn.1 rtn.2 rtn.3 rtn.4 rtn.5 rtn.6 rtn.7 rtn.8 rtn.9 rtn.10 > 1 a 1 1 2 3 4 5 6 7 8 9 10 > 2 b 2 1 2 3 4 5 6 7 8 9 10 > 3 c 3 1 2 3 4 5 6 7 8 9 10 > > What I wanted is something like this, conceptually: > > df > name type rtn > 1 a 1 list(1, 2, 3) > 2 b 2 list(4, 5, 6) > 3 c 3 list(7, 8, 9, 10) > > I discovered this in the R Language Definition manual: > > "A data frame can contain a list that is the same length as the other > components. The list can contain elements of differing lengths thereby > providing a data structure for ragged arrays. However, as of this > writing such arrays are not generally handled correctly. (2.3.2)" > > Is this still the case? What does 'not handled correctly' mean? Can I do > something like: > > sample(df$rtn, 1) > > If this isn't the way to do this, can you suggest the correct way? > > Thanks for your help and advice. > > -Kevin > > ______________________________________________ > 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.