Re: [R] Subassignments involving NAs in data frames

From: Thomas Lumley <tlumley_at_u.washington.edu>
Date: Fri 10 Jun 2005 - 04:23:03 EST

On Thu, 9 Jun 2005, McGehee, Robert wrote:

> I'm seeing some inconsistent behavior when re-assigning values in a data
> frame. The first assignment turns all of the 0s in my data frame to 2s,
> the second fails to do so.
>
>> df1 <- data.frame(a = c(NA, 0, 3, 4))
>> df2 <- data.frame(a = c(NA, 0, 0, 4))
>> df1[df1 == 0] <- 2 ## Works
>> df2[df2 == 0] <- 2
> Error: NAs are not allowed in subscripted assignments

Hmm. This looks like a bug to me.

> Checking an old news file I see this:
> o Subassignments involving NAs and with a replacement value of
> length > 1 are now disallowed. (They were handled
> inconsistently in R < 2.0.0, see PR#7210.) For data frames
> they are disallowed altogether, even for logical matrix indices
> (the only case which used to work).
>
> which leaves me to believe that the assignment for both df1 and df2
> should fail ("data frame ... disallowed altogether"), however that seems
> not to be the case, since the example works for df1.

Yes, I think the bug is that it works

> Also, the
> vectorized version works as expected (because the replacement value has
> a length of 1).
>
>> vec1 <- c(NA, 0, 3, 4)
>> vec2 <- c(NA, 0, 0, 4)
>> vec1[vec1 == 0] <- 2 ## Works
>> vec2[vec2 == 0] <- 2 ## Also works

I'm not sure that this is supposed to work, either, but it might be.

> Is this behavior for data frames intentional? What's the best
> alternative to df1[df1 == 0] <- 2 that doesn't fail in situations such
> as df2? A simple loop over columns?

df2[df2 %in% 0] is the recommended method.

         -thomas



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Jun 10 04:28:42 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:28 EST