- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check for equality of three columns by row in R?
To check for equality of three columns by row, we can use logical comparison of equality with double equal sign (==) and & operator.
For example, if we have a data frame called df that contains three columns say C1, C2, and C3 and we want to check for equality of these three columns then we can use below given command −
df$All_equal<-df$C1==df$C2 & df$C2==df$C3
Example 1
Following snippet creates a sample data frame −
x1<-rpois(20,1) x2<-rpois(20,1) x3<-rpois(20,1) df1<-data.frame(x1,x2,x3) df1
The following dataframe is created −
x1 x2 x3 1 4 0 1 2 1 2 1 3 0 3 1 4 0 2 0 5 0 2 3 6 1 1 2 7 1 1 0 8 1 0 0 9 0 0 0 10 0 1 0 11 1 2 4 12 2 1 4 13 0 1 0 14 0 3 1 15 1 0 1 16 0 0 0 17 1 0 1 18 0 1 0 19 0 1 0 20 1 0 1
In order to check whether values in all columns are equal for each row in df1, add the following code to the above snippet −
x1<-rpois(20,1) x2<-rpois(20,1) x3<-rpois(20,1) df1<-data.frame(x1,x2,x3) df1$All_equal<-df1$x1==df1$x2 & df1$x2==df1$x3 df1
Output
If you execute all the above given snippets as a single program, it generates the following output −
x1 x2 x3 All_equal 1 4 0 1 FALSE 2 1 2 1 FALSE 3 0 3 1 FALSE 4 0 2 0 FALSE 5 0 2 3 FALSE 6 1 1 2 FALSE 7 1 1 0 FALSE 8 1 0 0 FALSE 9 0 0 0 TRUE 10 0 1 0 FALSE 11 1 2 4 FALSE 12 2 1 4 FALSE 13 0 1 0 FALSE 14 0 3 1 FALSE 15 1 0 1 FALSE 16 0 0 0 TRUE 17 1 0 1 FALSE 18 0 1 0 FALSE 19 0 1 0 FALSE 20 1 0 1 FALSE
Example 2
Following snippet creates a sample data frame −
y1<-sample(0:1,20,replace=TRUE) y2<-sample(0:1,20,replace=TRUE) y3<-sample(0:1,20,replace=TRUE) df2<-data.frame(y1,y2,y3) df2
The following dataframe is created −
y1 y2 y3 1 0 1 0 2 0 1 1 3 1 1 1 4 1 0 0 5 1 1 1 6 0 0 0 7 1 0 0 8 1 1 0 9 1 0 1 10 1 0 1 11 1 1 1 12 0 0 1 13 0 1 0 14 0 1 0 15 0 0 0 16 0 1 1 17 0 0 1 18 1 0 0 19 1 1 0 20 0 0 1
In order to check whether values in all columns are equal for each row in df2, add the following code to the above snippet −
y1<-sample(0:1,20,replace=TRUE) y2<-sample(0:1,20,replace=TRUE) y3<-sample(0:1,20,replace=TRUE) df2<-data.frame(y1,y2,y3) df2$All_equal<-df2$y1==df2$y2 & df2$y2==df2$y3 df2
Output
If you execute all the above given snippets as a single program, it generates the following output −
y1 y2 y3 All_equal 1 0 1 0 FALSE 2 0 1 1 FALSE 3 1 1 1 TRUE 4 1 0 0 FALSE 5 1 1 1 TRUE 6 0 0 0 TRUE 7 1 0 0 FALSE 8 1 1 0 FALSE 9 1 0 1 FALSE 10 1 0 1 FALSE 11 1 1 1 TRUE 12 0 0 1 FALSE 13 0 1 0 FALSE 14 0 1 0 FALSE 15 0 0 0 TRUE 16 0 1 1 FALSE 17 0 0 1 FALSE 18 1 0 0 FALSE 19 1 1 0 FALSE 20 0 0 1 FALSE
- Related Articles
- How to aggregate matrix columns by row names in R?
- How to add all columns of an R data frame by row?
- How to find the row wise sum for n number of columns in R?
- How to find the row mean for columns in an R data frame by ignoring missing values?
- How to find the row mean for selected columns in R data frame?
- How to check matrix values equality with a vector values in R?
- How to find the row means in data.table object for columns having same name in R?
- How to find the row sum for each column by row name in an R matrix?
- MongoDB - How to check for equality in collection and in embedded document?
- Check two ArrayList for equality in Java
- Check two HashMap for equality in Java
- Check two numbers for equality in Java
- How to check String for equality under Unicode case folding in Golang?
- How to check equality of slices of bytes in Golang?
- How to find the row variance of columns having same name in R matrix?
