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
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. >
Henrique Dallazuanna
2010-Oct-06 17:12 UTC
[R] Empty data frame does not maintain column type
Try this: df <- data.frame(a=character(0),b=character(0), stringsAsFactors = FALSE) df[1,] <- c('a', 'a') On Wed, Oct 6, 2010 at 2:00 PM, N David Brown <hubdave@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@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
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