[R] Empty data frame does not maintain column type

Jeffrey Spies jspies at virginia.edu
Wed Oct 6 19:11:24 CEST 2010


This should do it:

df <- data.frame(a=character(0), b=character(0), stringsAsFactors=F)

because:

typeof(factor(0))

is "integer"

while:

typeof(character(0))

is "character".

Cheers,

Jeff.

On Wed, Oct 6, 2010 at 1:00 PM, N David Brown <hubdave at gmail.com> wrote:
> Does anyone know why a data frame created with empty character columns
> converts them to integer columns?
>
>> df<-data.frame(a=character(0),b=character(0))
>> df<-rbind(df,c("a","a"))
>> typeof(df[1,1])
> [1] "integer"
>
> AsIs doesn't help:
>
>> df<-data.frame(a=I(character(0)),b=I(character(0)))
>> df<-rbind(df,I(c("a","a")))
>> typeof(df[1,1])
> [1] "integer"
>
> Any suggestions on how to overcome this would be appreciated.
>
> Best wishes,
>
> David
>
> ______________________________________________
> 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