R:ifelse中的字符串列表 [英] R: string list in ifelse
本文介绍了R:ifelse中的字符串列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找与MySQL中的(...)'where'var'"语句相当的东西.
I am looking for something comparable to the "where 'var' in (...)" statement in MySQL.
我的代码如下:
data<-data.frame(id=10001:10030,cc1=rep(c("a","b","c"),10))
attach(data)
data$new<-ifelse(cc1=="a"|cc1=="b",1,0)
在ifelse中包含字符串列表而不是使用|的更好方法是什么??
What is a better way to include a list of strings in the ifelse instead of using | ?
类似的东西: ifelse(('a','b'),1,0)中的cc1
非常感谢!
推荐答案
ifelse(cc1 %in% c("a", "b"), 1, 0)
或者简单地:
as.numeric(data$cc1 %in% c("a", "b"))
尽管如此,请避免使用 attach
.它使事情变得混乱.
Avoid using attach
though. It makes things messy.
这篇关于R:ifelse中的字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文