How to find the unique rows based on some columns in R?

R ProgrammingServer Side ProgrammingProgramming

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
raja
Published on 12-Aug-2020 09:54:08
Advertisements