[R] Empty data frame does not maintain column type

N David Brown hubdave at gmail.com
Wed Oct 6 22:36:36 CEST 2010


Thanks to all three of you for responding. Brilliant answer there,
David - using the options() function as exemplified to set
stringsAsFactors=FALSE universally solved the issue.

Much appreciated, guys.


On 6 October 2010 18:27, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On Oct 6, 2010, at 1:00 PM, N David Brown wrote:
>
>> Does anyone know why a data frame created with empty character columns
>> converts them to integer columns?
>
> Quick answer: it's the strngsAsFactors demon but you have invoked that demon
> twice, Once with data.frame and the second rime with rbind. See if this
> helps:
>
>> zz <- factor(1:2)
>> typeof(zz)
> [1] "integer"  # it's the storage mode
>
>> df<-data.frame(a=character(0),b=character(0), stringsAsFactors=FALSE)
>> df<-rbind(df,c("a","a"))
>> typeof(df[1,1])
> [1] "integer"   # curses, foiled again!
>
> # I tried using strngsAsFactors=FALSE in the rbind call but got garbage:
>
>> df<-data.frame(a=character(0),b=character(0), stringsAsFactors=FALSE)
>> df<-rbind(df,c("a","a"), stringsAsFactors=FALSE)
>> df
>                 c..a....FALSE.. c..a....FALSE...1
> 1                              a                 a
> stringsAsFactors           FALSE             FALSE
>
> # You can set the global stringsAsFactors option since it appears that your
> # rbind invocation called out the devil again via the rbind.data.frame
> function.
> # So this is how you would prevent that behavior:
>
>> options(stringsAsFactors= FALSE)
>> df<-data.frame(a=character(0),b=character(0))
>
>
>> df<-rbind(df,c("a","a"))
>> str(df)
> 'data.frame':   1 obs. of  2 variables:
>  $ X.a.  : chr "a"
>  $ X.a..1: chr "a"
>
> --
> david
>
>
>>
>>> 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.
>
> David Winsemius, MD
> West Hartford, CT
>
>



More information about the R-help mailing list