[R] data.frame with variable-length list

Kevin Zembower kzembower at verizon.net
Sat Mar 9 01:49:24 CET 2013


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



More information about the R-help mailing list