【发布时间】:2015-02-19 06:56:54
【问题描述】:
假设我的第一个数据集是鸢尾花
data(iris)
head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa
我的第二个数据集包含如下所示的列类型
type
setosa
veranica
Big Rosa
virginica
我正在尝试在 iris 数据集中生成第六列,其值为 Yes 和 No,如果 iris$Species == 2ndDataset$type 中的值则为 Yes,否则为 no。
我有兴趣使用 %in% 选项来执行此操作。我试过但失败了,这就是我到目前为止所做的
iris[(as.character(iris$Species ) %in% as.character(2ndDataset$type)) , Result = 1, Result = 0]
需要帮助。
【问题讨论】:
-
我没有看到任何 if 语句。
as.character(iris$Species) %in% as.character(2ndDataset$type) + 0L不工作吗? -
@rawr,没有..得到一个错误
unused argument (Result = 1, Result = 0)我试图避免 if else 并使用 %in% -
如果您的第二个数据框称为
mydf,则此行适用于我。mutate(iris, check = as.character(Species) %in% as.character(mydf$type) + 0L) -
@jazzurro 这工作:) +1
-
但我没有使用那个参数,所以这就是为什么我没有收到错误
标签: r if-statement dplyr