a296180@mica.fmr.com
2002-May-20 10:46 UTC
[Rd] is.na<- coerces character vectors to be factors within dataframes (PR#1577)
I am not sure if this is a bug within is.na<- or if it lies deeper in the dataframe construction process. Indeed, perhaps it is not a bug at all (in which case I would suggest that the help page for NA be provided with a warning for unsuspecting users (like me)). When used on a character vector within a dataframe, is.na<- coerces the vector to factor.> x <- data.frame(var = LETTERS[1:3]) > x$var <- as.character(x$var) > xvar 1 A 2 B 3 C> is.character(x$var)[1] TRUE> is.na(x[[1]]) <- 2 > xvar 1 A 2 <NA> 3 C> is.character(x$var)[1] FALSE> is.factor(x$var)[1] TRUE>Interestingly enough, this coersion does not occur if you refer to x$var instead of x[[1]].> x <- data.frame(var = LETTERS[1:3]) > x$var <- as.character(x$var) > is.na(x$var) <- 2 > xvar 1 A 2 <NA> 3 C> is.character(x$var)[1] TRUE> is.factor(x$var)[1] FALSE>I could (ort of) imagine a story in which the coercision is the desired behavior -- by using is.na you are implicitly taking apart a dataframe and putting it back together and, when you make dataframes, character vectors are coerced to factor by default. But I can't come up with a story as to why x$var should be handled differently then x[[1]].> R.version_ platform sparc-sun-solaris2.6 arch sparc os solaris2.6 system sparc, solaris2.6 status major 1 minor 5.0 year 2002 month 04 day 29 language R>Thanks, David Kane -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._