[R] Question regarding to replace <NA>

Joshua Wiley jwiley.psych at gmail.com
Tue Nov 9 18:54:07 CET 2010


On Tue, Nov 9, 2010 at 9:43 AM, Joshua Wiley <jwiley.psych at gmail.com> wrote:
> On Tue, Nov 9, 2010 at 9:39 AM, Joshua Wiley <jwiley.psych at gmail.com> wrote:
>> Hi Kate,
>>
>> is.na() does not work on entire data frames.
>
> whoops, I did not mean that.  is.na() has a data frame method, but
> there are assignment issues (as you saw) when you use it that way.

Last correction (I promise).  Somehow I tested incorrectly which lead
to me to my two erroneous statements before.  is.na() works just fine
with data frames.  It was just the factor issue (as Phil said).

> x <- data.frame(a = 1:3, b = c(1, 2, NA), d = c(NA, 2, 3), e = factor(c(NA, NA, "1")))
> x
  a  b  d    e
1 1  1 NA <NA>
2 2  2  2 <NA>
3 3 NA  3    1

> x[is.na(x)] <- 0
Warning message:
In `[<-.factor`(`*tmp*`, thisvar, value = 0) :
  invalid factor level, NAs generated

> x # other NAs removed, but not the factor ones
  a b d    e
1 1 1 0 <NA>
2 2 2 2 <NA>
3 3 0 3    1

off-to-hide-under-a-rock-Josh



More information about the R-help mailing list