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 > dfa 1 NA 2 NaN 3 Inf 4 1 5 2 6 3>Thanks! Cheers!! Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [[alternative HTML version deleted]]
> df$a[is.infinite(df$a) | is.nan(df$a) ] <- NA > dfa 1 NA 2 NA 3 NA 4 1 5 2 6 3 On 5/26/11 3:18 PM, "Albert-Jan Roskam" <fomcl at yahoo.com> 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 > > >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >All right, but apart from the sanitation, the medicine, education, wine, >public >order, irrigation, roads, a fresh water system, and public health, what >have the >Romans ever done for us? >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > [[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.
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-JanThe 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)> dfa 1 NA 2 NA 3 NA 4 1 5 2 6 3 HTH, Marc Schwartz