What is equivalent to the Stata inlist () function in R?

Stata inlist allows us to refer to real or string values ​​of a variable. I was wondering if R such a function.

<strong> Examples:

I want to select eight states from the state variable (you can think of it as a state column in any data frame where state takes 50 string values ​​(states of the United States)).

  inlist(state,"NC","AZ","TX","NY","MA","CA","NJ") 

I want to select nine age values ​​from the age variable (you can think of it as an age column in any data frame where age takes numerical values ​​from 0 to 90).

  inlist(age,16, 24, 45, 54, 67,74, 78, 79, 85) 

Question:

 age<-c(0:10) # for this problem age takes values from 0 to 10 only data<-as.data.frame(age) # age is a variable of data frame data data$m<-ifelse(c(1,7,9)%in%data$age,0,1) # generate a variable m which takes value 0 if age is 1, 7, and 8 and 1, otherwise Expected output: age m 1 0 1 2 1 0 3 2 1 4 3 1 5 4 1 6 5 1 7 6 1 8 7 0 9 8 1 10 9 0 11 10 1 
+4
source share
1 answer

I think you want %in% :

 statevec <- c("NC","AZ","TX","NY","MA","CA","NJ") state <- c("AZ","VT") state %in% statevec ## TRUE FALSE agevec <- c(16, 24, 45, 54, 67,74, 78, 79, 85) age <- c(34,45) age %in% agevec ## FALSE TRUE 

edit : work on an updated question.

Copy from @NickCox link:

 inlist(z,a,b,...) Domain: all reals or all strings Range: 0 or 1 Description: returns 1 if z is a member of the remaining arguments; otherwise, returns 0. All arguments must be reals or all must be strings. The number of arguments is between 2 and 255 for reals and between 2 and 10 for strings. 

However, I'm not quite sure how this fits the original question. I don’t know Stata well enough to know if z be a vector or not: it doesn’t sound like that, and in this case the original question (considering z=state as a vector) does not make sense. If we think it could be a vector, then the answer will be as.numeric(state %in% statevec) - I think.

Edit: Update from Ananda

Using your updated data, here is one approach, again using %in% :

 data <- data.frame(age=0:10) within(data, { m <- as.numeric(!age %in% c(1, 7, 9)) }) age m 1 0 1 2 1 0 3 2 1 4 3 1 5 4 1 6 5 1 7 6 1 8 7 0 9 8 1 10 9 0 11 10 1 

This matches your expected result using ! (NOT) to invert the meaning of %in% . It seems to be a little behind what I thought about it (usually 0 = FALSE = "not in the list" and 1 = TRUE = "is in the list"), and my reading of Stata is a definition, but if that's what you want ...

Or you can use ifelse for greater potential flexibility (i.e. values ​​other than 0/1): replace within(data, { m <- ifelse(age %in% c(1, 7, 9),0,1)}) the code above.

+4
source

All Articles