Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if value is in data frame

Tags:

dataframe

r

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

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?

like image 816
Kewl Avatar asked Apr 05 '17 14:04

Kewl


People also ask

How do you check if a value exists in a data frame?

Use the in keyword to check if a value exists in a column of a DataFrame. Use the syntax value in pandas. DataFrame. column_name to determine if value exists in column_name of DataFrame .

How do you check if something is a DataFrame in Python?

Using isinstance() method. It is used to check particular data is RDD or dataframe. It returns the boolean value.


1 Answers

You could do:

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

OR with Reduce:

Reduce("|", A==1)

OR

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

OR

is.element(1,unlist(A))
like image 164
989 Avatar answered Sep 22 '22 05:09

989