Skip to content
Prev 361961 / 398506 Next

Subscripting problem with is.na()

Good point. I did not think about factors. Also your example raises another issue since column c is logical, but gets silently converted to numeric. This would seem to get the job done assuming the conversion is intended for numeric columns only:
a         b         c 
"numeric"  "factor" "logical"
a    b  c
1 1    A NA
2 0    b NA
3 2 <NA> NA

David C

-----Original Message-----
From: Bert Gunter [mailto:bgunter.4567 at gmail.com] 
Sent: Thursday, June 23, 2016 1:48 PM
To: David L Carlson
Cc: Ivan Calandra; R Help
Subject: Re: [R] Subscripting problem with is.na()

Not in general, David:

e.g.
a     b    c
[1,] FALSE FALSE TRUE
[2,]  TRUE FALSE TRUE
[3,] FALSE  TRUE TRUE
[1] NA NA NA NA NA
Warning message:
In `[<-.factor`(`*tmp*`, thisvar, value = 0) :
  invalid factor level, NA generated
a    b c
1 1    A 0
2 0    b 0
3 2 <NA> 0


The problem is the default conversion to factors and the replacement
operation for factors. So:
[1] "AsIs"  ## so NOT a factor
a b c
1 1 A 0
2 0 b 0
3 2 0 0

Of course the OP (and you) probably had a data frame of all numerics
in mind, so the problem doesn't arise. But I think one needs to make
the distinction and issue clear.

Cheers,
Bert





Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Thu, Jun 23, 2016 at 8:46 AM, David L Carlson <dcarlson at tamu.edu> wrote: