[R] data.frame with variable-length list
arun
smartpink111 at yahoo.com
Sat Mar 9 03:57:54 CET 2013
Hi,
You could also try:
df1<-data.frame(name=c("a","b","c"),type=c(1,2,3),rtn=as.array(list(1:3,4:6,7:10)))
A.K.
----- Original Message -----
From: Kevin Zembower <kzembower at verizon.net>
To: r-help at r-project.org
Cc:
Sent: Friday, March 8, 2013 7:49 PM
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 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.
More information about the R-help
mailing list