How to compare two columns in an R data frame for an exact match?

R ProgrammingServer Side ProgrammingProgramming

Sometimes analysis requires the user to check if values in two columns of an R data frame are exactly the same or not, this is helpful to analyze very large data frames if we suspect the comparative values in two columns. This can be easily done with the help of ifelse function.

Example

Consider the below data frame −

 Live Demo

x1<-sample(c("Spring","Winter","Autumn","Summer"),20,replace=TRUE)
y1<-sample(c("Spring","Winter","Autumn","Summer"),20,replace=TRUE)
df1<-data.frame(x1,y1)
df1

Output

     x1    y1
1 Spring Autumn
2 Winter Winter
3 Summer Summer
4 Autumn Autumn
5 Summer Autumn
6 Autumn Autumn
7 Summer Winter
8 Spring Spring
9 Summer Spring
10 Winter Winter
11 Autumn Summer
12 Autumn Summer
13 Spring Winter
14 Spring Winter
15 Spring Spring
16 Summer Autumn
17 Spring Winter
18 Winter Summer
19 Summer Spring
20 Spring Autumn

Determining whether each value in x1 and y1 are same or not −

Example

ifelse(df1$x1==df1$y1,"Yes","No")

Output

[1] "No" "Yes" "Yes" "Yes" "No" "Yes" "No" "Yes" "No" "Yes" "No" "No"
[13] "No" "No" "Yes" "No" "No" "No" "No" "No"

Example

 Live Demo

x2<-sample(c("Male","Female"),20,replace=TRUE)
y2<-sample(c("Male","Female"),20,replace=TRUE)
df2<-data.frame(x2,y2)
df2

Output

   x2       y2
1 Male    Female
2 Female  Male
3 Female  Female
4 Male    Male
5 Female  Female
6 Male    Female
7 Female  Female
8 Male    Male
9 Male    Female
10 Female Female
11 Female Female
12 Male   Female
13 Male   Male
14 Female Male
15 Male   Male
16 Female Female
17 Male   Male
18 Female Female
19 Male   Female
20 Male   Female

Determining whether each value in x2 and y2 are the same or not −

Example

> ifelse(df2$x2==df2$y2,"Yes","No")

Output

[1] "No" "No" "Yes" "Yes" "Yes" "No" "Yes" "Yes" "No" "Yes" "Yes" "No"
[13] "Yes" "No" "Yes" "Yes" "Yes" "Yes" "No" "No"
raja
Published on 07-Dec-2020 06:11:12
Advertisements