Does R have any similar commands to SAS IN?

4

Does R have some similar command to SAS IN?

Here is an example in SAS code:

IF variable1 IN (4,5,6,7) THEN variable2 = 1;

That is, if variable1 assumes values from 4 to 7, variable2 is assigned a value of 1.

    
asked by anonymous 12.01.2015 / 19:55

2 answers

3

You can also use the %in%

variavel1 <- 5  
if (variavel1 %in% c(4, 5, 6, 7)) {
   variavel2 <- 1
}

Functional sample

    
12.01.2015 / 20:08
2

Try the is.element function.

var1 <- 1
if (is.element(var1, c(1, 2, 4))) { 
    var2 <- 1
    print(var2)
}
    
12.01.2015 / 20:06