Does R have a similar command to SAS IN?

Asked

Viewed 93 times

4

R has some command similar to SAS IN?

Follow an example in SAS code:

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

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

2 answers

3


You can also use the operator %in%

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

Functional example

  • Both ways worked perfectly, thank you very much!

  • Hello Henry, don’t forget to accept the answer you deem most appropriate (all other useful answers can be presented with a positive vote).

  • 1

    That goes for yours, too other questions.

  • Thanks for the warning!

  • You are welcome Henrique. Whenever you have any questions or suggestions about the site you can use meta. Hugs.

2

Try the function is.element .

var1 <- 1
if (is.element(var1, c(1, 2, 4))) { 
    var2 <- 1
    print(var2)
}
  • What if var1 = 5, what value it takes var2?

  • @Gradually, I edited the answer. Now var2 only prints if the condition is true. Anyway if the element is not in the list it will return false. example: is.element(5, c(1, 2, 4)).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.