[R] NaN, Inf to NA

Kenn Konstabel lebatsnok at gmail.com
Fri May 27 14:10:19 CEST 2011


On Fri, May 27, 2011 at 11:27 AM, Albert-Jan Roskam <fomcl at yahoo.com> wrote:
> Aha! Thank you very much for that clarification! It would be much more user
> friendly if R generated a NotImplementedError or something similar. The 'garbage
> results' are pretty misleading, esp. to a novice.
>
> I wanted to recode every NaN and Inf value of an entire data.frame to NA. The
> data.frame also includes character variables. So the following might work (?)
> (Can't test it here)
>
> ditch <- function(x) ifelse(is.infinite(x) | is.nan(x), NA, x)
> df <- apply(df, 2, ditch)
>

the result of apply will be a matrix (not a data frame). But you could
try subscripting with a matrix e.g.:

 df <- data.frame(a=c(NA, NaN, Inf, 1:3), b=c(2,NaN))
 df[apply(df, 2, Negate(is.finite))] <- NA

# or lapply version with subset assignment
df <- data.frame(a=c(NA, NaN, Inf, 1:3), b=c(2,NaN))
df[] <- lapply(df, function(x) ifelse(is.finite(x), x, NA))


KK






>
>
>
>
> ________________________________
> From: William Dunlap <wdunlap at tibco.com>
>
> Cc: R Mailing List <r-help at r-project.org>
> Sent: Fri, May 27, 2011 12:57:01 AM
> Subject: RE: [R] NaN, Inf to NA
>
> I think the source of the OP's problem is that
> while things like df>30 and is.na(df) return
> a logical matrix with the dimensions of the
> data.frame df, both is.infinite(df) and is.nan(df)
> return a logical vector as long as the number
> of columns of df.  (`>` and is.na have data.frame
> methods but is.infinite and is.nan do not: the latter
> give garbage results for data.frames.)
>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
>
>> -----Original Message-----
>> From: r-help-bounces at r-project.org
>> [mailto:r-help-bounces at r-project.org] On Behalf Of Marc Schwartz
>> Sent: Thursday, May 26, 2011 2:15 PM
>> To: Albert-Jan Roskam
>> Cc: R Mailing List
>> Subject: Re: [R] NaN, Inf to NA
>>
>> 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
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list