Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assign a value, if a number is in between two numbers

Tags:

r

if-statement

Im trying to assign the value of -1, to every number in my vector that is inbetween 2 and 5.

I thought an if - then statement would work. I am having some trouble. I dont think (2<x<5) is right but I am not sure how to write inbetween in R. Can anyone help? Thanks

x <- c(3.2,6,7.8,1,3,2.5)
if (2<x<5){
    cat(-1)
} else {
    cat (x)
}
like image 768
Bill Avatar asked Oct 18 '12 02:10

Bill


People also ask

How do I check if a value is falling between two numbers in Excel?

Check if a cell value is between two values with formula Select a blank cell which you need to display the result, enter formula =IF(AND(B2>A2,B2<A3),"Yes","No") into the Formula Bar and then press the Enter key.

How do you return a value if a given value exists in a certain range in Excel?

Return a value if a given value exists in a certain range by using a formula. Please apply the following formula to return a value if a given value exists in a certain range in Excel. 1. Select a blank cell, enter formula =VLOOKUP(E2,A2:C8,3, TRUE) into the Formula Bar and then press the Enter key.


3 Answers

There are a number of syntax error in your code.

Try using findInterval

x[findInterval(x, c(2,5)) == 1L] <- -1
x
## [1]  -1.0  6.0  7.8  1.0 -1.0 -1.0

read ?findInterval for more details on the use of findInterval

You could also use replace

replace(x, x > 2 & x < 5, -1)

Note that

  • for 2<x<5 you need to write x > 2 & x < 5
  • cat will output to the console or a file / connection. It won't assign anything.
like image 131
mnel Avatar answered Oct 10 '22 20:10

mnel


You probably just want to replace those elements with -1.

> x[x > 2 & x < 5] <- -1; x
[1] -1.0  6.0  7.8  1.0 -1.0 -1.0

You could also use ifelse.

> ifelse(x > 2 & x < 5, -1, x)
[1] -1.0  6.0  7.8  1.0 -1.0 -1.0
like image 14
Aaron left Stack Overflow Avatar answered Oct 10 '22 21:10

Aaron left Stack Overflow


I compared the solutions with microbenchmark:

library(microbenchmark)
library(TeachingDemos)

x = runif(100000) * 1000
microbenchmark(200 %<% x %<% 500
               , x > 200 & x < 500
               , findInterval(x, c(200, 500)) == 1
               , findInterval(x, c(200, 500)) == 1L
               , times = 1000L
               )

Here are the results:

                               expr       min        lq      mean    median        uq       max neval
                  200 %<% x %<% 500 17.089646 17.747136 20.477348 18.910708 21.302945 113.71473  1000
                  x > 200 & x < 500  6.774338  7.092153  8.746814  7.233512  8.284603 103.64097  1000
  findInterval(x, c(200, 500)) == 1  3.578305  3.734023  5.724540  3.933615  6.777687  91.09649  1000
 findInterval(x, c(200, 500)) == 1L  2.042831  2.115266  2.920081  2.227426  2.434677  85.99866  1000

You should take findInterval. Please consider to compare it to 1L instead of 1. It is nearly twice as fast.

like image 3
Dirk Avatar answered Oct 10 '22 21:10

Dirk