[R] Inconvenient behavior of as.data.frame() for lists without names
Magnus Thor Torfason
zulutime.net at gmail.com
Thu Sep 16 00:10:05 CEST 2010
Hi all,
I ran into a small issue when converting a list of vectors to a data
frame. The Issue I'm having is described by the snippet below:
#########################################################
# Convert a list of vectors into a data.frame
strlen = 256
s.long.a = paste( letters[1+(0:strlen %% 26)], collapse="")
s.long.b = paste( letters[1+(strlen:0 %% 26)], collapse="")
v.long.a = rep(s.long.a, 2)
v.long.b = rep(s.long.b, 2)
# Convert when the list has no names for its elements
my.list = list(v.long.a, v.long.b)
my.df = as.data.frame(my.list)
# Here we get an error
my.df
# This solves the problem
names(my.list) = c("a","b")
my.fixed.df = as.data.frame(my.list)
my.fixed.df
#########################################################
In short, the problem is that when there are no names attached to the
elements of the list, it creates very long names - if the elements of
the vectors themselves are long. And further, that names that are in
some since disallowed (can't be printed, for one), are silently injected
into a data.frame, leading to an error later on.
Better would be to error out in as.data.frame
Best would be if way of generating default names in this function would
be intelligent enough to never create names longer than - say 30
characters. Of course, explicit names should be honored.
Anyway, that's my thoughts on this issue. No patch attached, and I will
work around this, but at least it is out there now.
Best,
Magnus Thor
More information about the R-help
mailing list