[R] Convert list to data frame while controlling column types

Steve Lianoglou mailinglist.honeypot at gmail.com
Fri Aug 21 17:54:12 CEST 2009

Hi Allie,

On Aug 21, 2009, at 11:47 AM, Alexander Shenkin wrote:

> Hello all,
> I have a list which I'd like to convert to a data frame, while
> maintaining control of the columns' data types (akin to the colClasses
> argument in read.table).  My numeric columns, for example, are getting
> converted to factors by as.data.frame.  Is there a way to do this, or
> will I have to do as I am doing right now: allow as.data.frame to  
> coerce
> column-types as it sees fit, and then convert them back manually?

This doesn't sound right ... are there characters buried in your  
numeric columns somewhere that might be causing this?

I'm pretty sure this shouldn't happen, and a small test case here goes  
along with my intuition:

R> a <- list(a=1:10, b=rnorm(10), c=LETTERS[1:10])
R> df <- as.data.frame(a)
R> sapply(df, is.factor)
     a     b     c

Can you check to see if your data's wonky somehow?


Steve Lianoglou
Graduate Student: Computational Systems Biology
   |  Memorial Sloan-Kettering Cancer Center
   |  Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact

More information about the R-help mailing list