[R] list to data.frame

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Thu Dec 19 19:50:07 CET 2002


On Thu, 19 Dec 2002 Benjamin.STABLER at odot.state.or.us wrote:

> I have a list of 102 vectors all of the same type and length called
> time.by.orig.  I can't data.frame(time.by.orig) but I can
> data.frame(time.by.orig[1:length(time.by.orig)]).  Why is this?  Thanks for
> your help.

You don't have a list: you have a one-dimensional array of mode list.
Subsetting drops the dimension and gives you a list.

Please pay attention to the messages: note the attributes of your object
and that the error message said it was a list.

> str(time.by.orig)
> List of 102
>  $ 1  : num [1:102]  1.34 17.39 14.36 14.22  7.56 ...
>  $ 2  : num [1:102] 17.5  0.7 17.7 12.4 10.4 ...
>  $ 3  : num [1:102] 14.063 17.568  0.754  8.065 15.100 ...
>  $ 4  : num [1:102] 13.940 12.423  7.546  0.766 14.977 ...
>  $ 100: num [1:102]  7.722 10.400 15.049 14.915  0.467 ...
>  $ 101: num [1:102]  5.68 13.99 13.90 13.77  4.01 ...
>  $ 102: num [1:102]  6.67 11.86 14.88 14.75  1.68 ...
>  ......
>  - attr(*, "dim")= int 102
>  - attr(*, "dimnames")=List of 1
>   ..$ : chr [1:102] "1" "2" "3" "4" ...
>
>
> > data.frame(time.by.orig)
> Error in as.data.frame.default(x[[i]], optional = TRUE) :
>         can't coerce array into a data.frame
>
> > data.frame(time.by.orig[1:length(time.by.orig)])
>  -Works fine

Irrelevant earlier message deleted: please don't waste bandwidth like
that.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list