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?
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 .
Using isinstance() method. It is used to check particular data is RDD or dataframe. It returns the boolean value.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With