检查值是否在数据框中 [英] Check if value is in data frame

查看:43
本文介绍了检查值是否在数据框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查特定值是否在数据框中的任何地方。

I'm trying to check if a specific value is anywhere in a data frame.

我知道%in%运算符应允许我执行此操作,但是它似乎不适用于应用于整个数据帧的预期方式:

I know the %in% operator should allow me to do this, but it doesn't seem to work the way I would expect when applying to a whole data frame:

A = data.frame(B=c(1,2,3,4), C=c(5,6,7,8))
1 %in% A

[1] FALSE

但是如果我将此值应用于特定列是按我期望的方式工作的:

But if I apply this to the specific column the value is in it works the way I expect:

1 %in% A$C

[1] TRUE

检查值是否在数据帧中的任何位置的正确方法是什么?

What is the proper way of checking if a value is anywhere in a data frame?

推荐答案

您可以这样做:

any(A==1)
#[1] TRUE

或与 Reduce

Reduce("|", A==1)

OR

length(which(A==1))>0

OR

is.element(1,unlist(A))

这篇关于检查值是否在数据框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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