(PR#1577) [Rd] is.na<- coerces character vectors to be factors

a296180@mica.fmr.com a296180@mica.fmr.com
Mon, 20 May 2002 19:25:10 +0200 (MET DST)


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
> x
   var
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._