[R] Handling NA values in a if statement

Marc Schwartz marc_schwartz at me.com
Sat Apr 18 00:43:57 CEST 2015


On Apr 17, 2015, at 5:23 PM, Luigi Marongiu <marongiu.luigi at gmail.com> wrote:
> 
> Dear all,
> I have a vector with a certain range of values including infinity and
> NA. I would like to remove the values that are outside a given range
> (lower level = ll and upper level = ul) but I am getting the error due
> to the NA values (missing value where TRUE/FALSE needed). I then
> included the !is.na() but now the resulting error is all NA, as in the
> example.
> In addition, here I have implemented a for loop to scan all the
> elements of the vector, but I should be able to use the sapply();
> however I don't know how to send the ll and ul arguments to sapply().
> Could you please help?
> best regards
> Luigi
> 
> EXAMPLE
> 
> x <- c(-Inf,  Inf,    NA,    5.9,    6.08,    5281391136138.75,
> 4.35,    4.79,
>       9474097322.96,    3.64,    16.42,    -12211.11,    4.37,
> -1097.79,    4.78,
>       3.71,    32.59,    4.01,    35.36,    3.17,    1.61,
> -3678.28,    2.9,    4.67,
>       4.1,    348410866.78,    5.35,    4.3101519459837E+016,
> 1467030866.75,
>       1.10376094956278E+018,    32.55,    1.17,    5339028670388.94,
>  34.14,
>       33205967009.57,    4.42,    1.76,    7.08,    -8428.84,
> -113491.08,    17.81)
> ll <- 1
> ul <- 45
> 
> clipper <- function(x, ll, ul) {
>  for(i in 1:length(x)) {
>    if(x[i] < ll) {
>      x[i] <- NA
>    } else if(x[i] > ul) {
>      x[i] <- NA
>    } else {
>      x[i] <- x[i]
>    }
>  }
> return(x)
> }
> (X<-clipper(x, ll, ul))
>> missing value where TRUE/FALSE needed
> 
> 
> clipper <- function(x, ll, ul) {
>  for(i in 1:length(x)) {
>    if(!is.na(x[i]) < ll) {
>      x[i] <- NA
>    } else if(!is.na(x[i]) > ul) {
>      x[i] <- NA
>    } else {
>      x[i] <- x[i]
>    }
>  }
> return(x)
> }
> (X<-clipper(x, ll, ul))
> 
> [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
> NA NA NA NA NA
> [28] NA NA NA NA NA NA NA NA NA NA NA NA NA NA


Hi,

Something along the lines of:

> subset(x, is.finite(x) & (x > ll) & (x < ul))
 [1]  5.90  6.08  4.35  4.79  3.64 16.42  4.37  4.78  3.71 32.59  4.01
[12] 35.36  3.17  1.61  2.90  4.67  4.10  5.35 32.55  1.17 34.14  4.42
[23]  1.76  7.08 17.81

or:

> x[is.finite(x) & (x > ll) & (x < ul)]
 [1]  5.90  6.08  4.35  4.79  3.64 16.42  4.37  4.78  3.71 32.59  4.01
[12] 35.36  3.17  1.61  2.90  4.67  4.10  5.35 32.55  1.17 34.14  4.42
[23]  1.76  7.08 17.81


See ?subset and ?is.finite:

> is.finite(x)
 [1] FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
[12]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
[23]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
[34]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE


Regards,

Marc Schwartz



More information about the R-help mailing list