Suppose you have a data frame called data with two identical columns:
A B 1 1 2 2 3 3 4 4
How can I check if these two columns are identical and return one logical value to indicate it? A very basic pseudocode is:
if(data$A == data$B) { print("Column A and B are identical") }
I have been messing around with this for a bit and haven't found a way to do it that doesn't seem unnecessarily convoluted. Thanks.
Use the duplicated() function to create a vector that indicates which columns are identical. Optionall, show the names of the duplicated columns using the colnames() function.
We can compare two columns in R by using ifelse(). This statement is used to check the condition given and return the data accordingly.
You can use the function all_equal from the package dplyr . The function returns TRUE if the two data frames are identical, otherwise a character vector describing the reasons why they are not equal.
setequal() function in R Language is used to check if two objects are equal.
You could use identical
identical(DT[['A']],DT[['B']])
You could use all()
:
> data <- data.frame(A=c(1,2,3,4), B=c(1,2,3,4)) > all(data$A == data$B) [1] TRUE
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