Skip to content
Prev 237118 / 398500 Next

Empty data frame does not maintain column type

On Oct 6, 2010, at 1:00 PM, N David Brown wrote:

            
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"