首页 > 解决方案 > 检查 R 列中是否存在值向量,然后返回单个 True 值

问题描述

如果我有这个数据框:

df <- tibble(code = c("ABC", "DEF", "GHI"))

我有以下价值观:

my_values <- c("ABC","GHI") 

如何检查我的对象 my_values 是否在数据框 df 中找到并返回单个 TRUE 布尔值?

我不在乎这些值出现了多少次,如果 my_values 中的任何或所有值出现在名为 code 的数据框列中,我只想返回一个奇异的 TRUE 布尔值?

My desired result is the single boolean TRUE - i'm stuck on this and have been using the %in% operator but to no avail.

标签: rdataframedplyr

解决方案


怎么样

any(df$code %in% my_values) 

也许

any(my_values  %in% df$code) 

推荐阅读