Stata's inlist allows us to refer to the real or string values of a variable. I was wondering whether R
has such a function.
Examples:
I want to choose eight states from the variable state
(you can think this as column state
in any dataframe where state
takes 50 string values (states of United States)).
inlist(state,"NC","AZ","TX","NY","MA","CA","NJ")
I want to choose nine values of age from the variable age
(you can think this as column age
in any dataframe 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
match()
or%in%
but am not too familiar with theinlist
function from Stata. – Berglundstate
andage
and showed the expected output ... – Bobbieinlist()
is a function, and not a command. – Spotlightinlist()
as a Stata function. – Spotlight