R中的Stata函数inlist()等价于什么? [英] What is the equivalent of Stata function inlist() in R?

查看:162
本文介绍了R中的Stata函数inlist()等价于什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Stata的 inlist 允许我们引用实数值或字符串值变量我想知道 R 是否具有这样的功能.

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.

示例:

我想从变量 state 中选择八个状态(您可以将其视为任何状态为 state 的字符串的 state 列值(美国各州).

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")

我想从变量 age 中选择年龄的九个值(您可以将其视为在 age 需要的任何数据框中的 age 列数值从0到90).

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) 

问题:

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

推荐答案

我认为您想要%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

编辑:处理更新的问题.

从@NickCox的链接复制:

Copying from @NickCox's 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.

但是,我不太确定这与原始问题如何匹配.我不太了解Stata,无法知道 z 是否可以是矢量:听起来不是那样,在这种情况下,原来的问题(考虑 z = state 作为向量)没有任何意义.如果我们认为它可以是向量,那么答案将是 as.numeric(state%in%statevec)-我认为.

However, I'm not quite sure how this matches up with the original question. I don't know Stata well enough to know if z can be a vector or not: it doesn't sound that way, in which case the original question (considering z=state as a vector) doesn't make sense. If we consider that it can be a vector then the answer would be as.numeric(state %in% statevec) -- I think.

由Ananda更新

Update by Ananda

使用更新的数据,这是一种方法,再次使用%in%:

Using your updated data, here's 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

这可以通过使用(NOT)反转%in%的含义来匹配您的预期输出.这似乎与我的思考方式有些倒退(通常,0 = FALSE =不在列表中",而1 = TRUE =是在列表中")和我对Stata的定义的阅读,但是如果您要的是...

This matches your expected output, by using ! (NOT) to invert the sense of %in%. It seems to be a little backwards from the way I would think about it (normally, 0=FALSE="is not in the list" and 1=TRUE="is in the list") and my reading of Stata's definition, but if it's what you want ...

或者可以使用 ifelse 获得更大的潜在灵活性(即,非0/1的值):用(data,{m<-ifelse(age%in%c(1,7,9),0,1)}).

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

这篇关于R中的Stata函数inlist()等价于什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆