ripley@stats.ox.ac.uk
2002-May-20 11:46 UTC
(PR#1577) [Rd] is.na<- coerces character vectors to be factors
The inconsistency is that you use $<- to set the column, then [[<- to change it. Had you tried to set the column by x[[1]] <- as.character(x[[1]]) you would have seen the problem immediately (it does not work as you would have intended). If you want to be sure to turn off conversion to factor, you need to set the column to class "AsIs". My belief is that will behave consistently.> x <- data.frame(var = I(LETTERS[1:3])) > is.na(x[[1]]) <- 2 > xvar 1 A 2 <NA> 3 C> is.character(x$var)[1] TRUE> is.factor(x$var)[1] FALSE In any case, it is nothing to do with is.na<-:> x <- data.frame(var = LETTERS[1:3]) > x$var <- as.character(x$var) > > x[[1]][2] <- "3" > xvar 1 A 2 3 3 C> is.character(x$var)[1] FALSE You'll see that [[<-.data.frame contains if (!inherits(value, "data.frame")) value <- as.data.frame(value) and that is where the coercion is done. It really isn't possible to tell your intentions if you replace a whole column of a data frame. Having said all that, the source says if(nargs() < 4) { ## really ambiguous, but follow common use as if list ## el(x,i) <- value is the preferred approach and S4 does not do the conversion. I don't understand the comment, as> get("el<-").Primitive("[[<-") and so el<- just calls [[<- .... On Mon, 20 May 2002 a296180@mica.fmr.com wrote:> 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)).Not appropriate!> 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) > > x > var > 1 A > 2 B > 3 C > > is.character(x$var) > [1] TRUE > > is.na(x[[1]]) <- 2 > > x > var > 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 > > x > var > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-- Brian D. Ripley, ripley@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
a296180@mica.fmr.com
2002-May-20 17:25 UTC
(PR#1577) [Rd] is.na<- coerces character vectors to be factors
Thanks for taking the time and trouble to explain this to me. ripley@stats.ox.ac.uk writes: > You'll see that [[<-.data.frame contains > > if (!inherits(value, "data.frame")) > value <- as.data.frame(value) > > and that is where the coercion is done. It really isn't possible to tell > your intentions if you replace a whole column of a data frame. What is the appropriate way to change the value in a single row in a dataframe to NA without having to "replace a whole column"? That is, how can I change the value "B" in row 2 to being NA while keeping var as a vector of characters?> x <- data.frame(var = LETTERS[1:3]) > x$var <- as.character(x$var)One obvious answer is to use simple assignment.> x[2,1] <- NA > xvar 1 A 2 <NA> 3 C> is.character(x$var)[1] TRUE>This has the desired effect. Moreover, something like this would work with my merge.data.frame problem. However, I have been operating under the (mistaken?) impression that assigning NA directly was A Bad Thing. > On Mon, 20 May 2002 a296180@mica.fmr.com wrote: > > > 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)). > > Not appropriate! Yes. I see that now. Regards, 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Apparently Analagous Threads
- is.na<- coerces character vectors to be factors within dataframes (PR#1577)
- Strange behavior with saved character vectors containing a slash
- subsetting with NA's
- Bug when trying to save a character vector with an embedded / (PR#1115)
- package argument to library as string