Checking the same columns in a data frame in R

Suppose you have a data frame called data with two identical columns:

AB 1 1 2 2 3 3 4 4 

How to check if these two columns are identical and return the boolean value one to indicate it? Very simple pseudo code:

 if(data$A == data$B) { print("Column A and B are identical") } 

I talked a bit with this and did not find a way to do this, which does not seem unnecessarily confusing. Thanks.

+6
source share
3 answers

You can use identical

 identical(DT[['A']],DT[['B']]) 
+11
source

You can use all() :

 > data <- data.frame(A=c(1,2,3,4), B=c(1,2,3,4)) > all(data$A == data$B) [1] TRUE 
+14
source

This may be redundant for your problem, but you can also look into compare() from the "compare" pacakge. Consider the following examples:

 > data <- data.frame(A = c(1, 2, 3, 4), B = c(1, 2, 3, 4)) > compare(data[1], data[2]) ## Should be false FALSE [TRUE] > compare(data[1], data[2], ignoreNames = TRUE) # Allow different names TRUE dropped names > data <- data.frame(A = c(1, 2, 3, 4), B = as.character(c(1, 2, 3, 4))) > str(data) ## "B" is now a factor, so use `coerce` to test for equality 'data.frame': 4 obs. of 2 variables: $ A: num 1 2 3 4 $ B: Factor w/ 4 levels "1","2","3","4": 1 2 3 4 > compare(data[1], data[2], ignoreNames = TRUE, coerce = TRUE) TRUE [A] coerced from <factor> to <numeric> dropped names 

There is a common boolean argument allowAll , which can be set to TRUE to allow the compare function to try various transformations to check for equality.

+3
source

All Articles