[R] NaN, Inf to NA
Marc Schwartz
marc_schwartz at me.com
Thu May 26 23:15:29 CEST 2011
On May 26, 2011, at 3:18 PM, Albert-Jan Roskam wrote:
> Hi,
>
> I want to recode all Inf and NaN values to NA, but I;m surprised to see the
> result of the following code. Could anybody enlighten me about this?
>
>> df <- data.frame(a=c(NA, NaN, Inf, 1:3))
>> df[is.infinite(df) | is.nan(df)] <- NA
>> df
> a
> 1 NA
> 2 NaN
> 3 Inf
> 4 1
> 5 2
> 6 3
>>
>
>
> Thanks!
>
> Cheers!!
> Albert-Jan
The canonical way is to use is.na() to assign the NA value based upon a condition. See ?is.na for more information.
is.na(df$a) <- !is.finite(df$a)
> df
a
1 NA
2 NA
3 NA
4 1
5 2
6 3
HTH,
Marc Schwartz
More information about the R-help
mailing list