Search code examples
rif-statementcomparerowcategorical-data

How can I compare and represent the categorical data of my two columns with 0 if the rows are the same and 1 if not, using R


How can I compare the categorical data of my two columns and visualize it with a 0 if the row is the same for the two columns and if different 1.


Solution

  • If the categorical columns of your dataframe are named x and y and have the same levels you can assign a new column to your dataframe with the result of comparing the two columns using the ifelse function.

    df$match <- ifelse(df$x == df$y, 0, 1)