Re: [R] Unexpected behaviour as.data.frame

From: Jan van der Laan <eoos_at_dds.nl>
Date: Sun, 15 May 2011 13:41:03 +0200

Forget I asked. There was a typo in my example (stringsAsFactor instead of stringAsFactors) which explained the difference. My apologies.

My second question however still stands: How does on create a data.frame with given column types and given dimensions? Thanks.

Regards,
Jan

Quoting Jan van der Laan <rhelp_at_eoos.dds.nl>:

> I use the following code to create two data.frames d1 and d2 from a list:
>
> types <- c("integer", "character", "double")
> nlines <- 10
> d1 <- as.data.frame(lapply(types, do.call, list(nlines)),
> stringsAsFactor=FALSE)
> l2 <- lapply(types, do.call, list(nlines))
> d2 <- as.data.frame(l2, stringsAsFactors=FALSE)
>
> I would expect d1 and d2 to be the same, however, in d1 the second
> column is a factor while in d2 it is a character (which I would expect):
>
>> str(d1)
> 'data.frame': 10 obs. of 3 variables:
> $ c.0L..0L..0L..0L..0L..0L..0L..0L..0L..0L.: int 0 0 0 0 0 0 0 0 0 0
> $ c........................................: Factor w/ 1 level "": 1 1
> 1 1 1 1 1 1 1 1
> $ c.0..0..0..0..0..0..0..0..0..0. : num 0 0 0 0 0 0 0 0 0 0
>> str(d2)
> 'data.frame': 10 obs. of 3 variables:
> $ c.0L..0L..0L..0L..0L..0L..0L..0L..0L..0L.: int 0 0 0 0 0 0 0 0 0 0
> $ c........................................: chr "" "" "" "" ...
> $ c.0..0..0..0..0..0..0..0..0..0. : num 0 0 0 0 0 0 0 0 0 0
>
>
> As different but related question: I use the commands above to create
> an 'empty' data.frame with specified column types and dimensions. I
> need this data.frame to pass on to my c++ routines. Is there a more
> simple/elegant way of creating this data.frame?
>
> Regards,
>
> Jan
>
>
> PS:
> I am running R on 64 bit Ubuntu 11.04:
>
>> sessionInfo()
> R version 2.12.1 (2010-12-16)
> Platform: x86_64-pc-linux-gnu (64-bit)
>
> locale:
> [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
> [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
> [5] LC_MONETARY=C LC_MESSAGES=en_US.UTF-8
> [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
> [9] LC_ADDRESS=C LC_TELEPHONE=C
> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats graphics grDevices utils datasets methods base



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. Received on Sun 15 May 2011 - 12:55:51 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 15 May 2011 - 13:10:06 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive