
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 find the unique rows based on some columns in R?
Especially when the experimental conditions are same then we expect some of the row values for some columns to be the same, it is also done on purpose while designing the experiments to check the fixed effect of variables. If we want to determine the unique rows then it can be done by using unique function in R.
Example
Consider the below data frame −
> x1<-rep(c(1,2,3,4,5),each=4) > x2<-rep(c(1,2,3,4,5),times=c(2,4,4,3,7)) > x3<-LETTERS[1:20] > df<-data.frame(x1,x2,x3) > df x1 x2 x3 1 1 1 A 2 1 1 B 3 1 2 C 4 1 2 D 5 2 2 E 6 2 2 F 7 2 3 G 8 2 3 H 9 3 3 I 10 3 3 J 11 3 4 K 12 3 4 L 13 4 4 M 14 4 5 N 15 4 5 O 16 4 5 P 17 5 5 Q 18 5 5 R 19 5 5 S 20 5 5 T > df[row.names(unique(df[,c("x1", "x2")])),] x1 x2 x3 1 1 1 A 3 1 2 C 5 2 2 E 7 2 3 G 9 3 3 I 11 3 4 K 13 4 4 M 14 4 5 N 17 5 5 Q
Let’s have a look at another example −
> y1<-rep(c(letters[1:4]),times=5) > y2<-rep(c(letters[1:4]),each=5) > y3<-1:20 > df_y<-data.frame(y1,y2,y3) > df_y y1 y2 y3 1 a a 1 2 b a 2 3 c a 3 4 d a 4 5 a a 5 6 b b 6 7 c b 7 8 d b 8 9 a b 9 10 b b 10 11 c c 11 12 d c 12 13 a c 13 14 b c 14 15 c c 15 16 d d 16 17 a d 17 18 b d 18 19 c d 19 20 d d 20 > df_y[row.names(unique(df_y[,c("y1", "y2")])),] y1 y2 y3 1 a a 1 2 b a 2 3 c a 3 4 d a 4 6 b b 6 7 c b 7 8 d b 8 9 a b 9 11 c c 11 12 d c 12 13 a c 13 14 b c 14 16 d d 16 17 a d 17 18 b d 18 19 c d 19
- Related Questions & Answers
- How to find the sum of rows of a column based on multiple columns in R data frame?
- How to find the number of unique values of multiple categorical columns based on one categorical column in R?
- How to find the frequency for all columns based on a condition in R?
- How to find the unique rows in an R data frame?
- How to subset rows based on criterion of multiple numerical columns in R data frame?
- How to find the sum of rows of a column based on multiple columns in R’s data.table object?
- How to find the mean of multiple columns based on a character column in R?
- Find the mean of multiple columns based on multiple grouping columns in R data frame.
- How to find the sum of numerical columns based on the combination of values in\ncategorical columns in R data frame?
- How to find the index of an element in a matrix column based on some condition in R?
- Find the frequency of exclusive group combinations based on multiple categorical columns in R.
- How to find the minimum for each row based on few columns in an R data frame?
- How to find the range of columns if some columns are categorical in R data frame?
- How to select data frame columns based on their class in R?
- How to select data.table object columns based on their class in R?
Advertisements