在数据框单元格中使用%in%运算符

时间:2016-12-13 13:59:44

标签: r dataframe subset comparison-operators

使用%in%运算符,这个子集化操作wantedSubset的等价物是什么?

dataset <- data.frame(
  "V1"=c(1,2,3,4,5,6,7,8),
  "V2"=c("green","yellow","blue","red","purple","black","white","gray")
)

# subset all cells containing the value 3 or the value "blue"
wantedSubset <- dataset == 3 | dataset == "blue"

我希望使用wantedSubset运算符以不同的方式执行%in%操作。怎么&amp;为什么我能做到这一点? (似乎%in应用于整个列而不是每个单元格值?)

# will not return wanted result
dataset %in% c("3","blue")

0 个答案:

没有答案