How to check if two data frames same or not in R?

R ProgrammingServer Side ProgrammingProgramming

Two data frames can be same if the column names, row names and all the values in the data frame are exactly same. We might to check this for data frames that we expect to be same, for example, if we have two data sets each one of have same number of rows, same number of columns, same data type for each of the columns, and the data view shows that values are same then it is worth checking whether the complete data sets are same or not. To do this checking in R, we can use identical function.

Examples

 Live Demo

df1<-data.frame(x1=rnorm(20),x2=letters[1:20])
df1

Output

     x1 x2
1 -0.09240552 a
2 -0.86521420 b
3 -0.69926988 c
4 1.24016055 d
5 2.75433272 e
6 0.49542119 f
7 2.10237466 g
8 0.41385580 h
9 0.82198425 i
10 0.41587660 j
11 3.32618703 k
12 -0.56282543 l
13 -0.39042007 m
14 0.78080197 n
15 0.23957177 o
16 -1.92511201 p
17 0.57842631 q
18 -0.82922740 r
19 2.04836392 s
20 0.24616976 t

Example

 Live Demo

df2<-data.frame(y1=rnorm(20),y2=letters[1:20])
df2

Output

   y1    y2
1 0.02352228 a
2 -0.73437399 b
3 -0.64123651 c
4 0.09660536 d
5 -0.52194846 e
6 0.95606767 f
7 -0.37678881 g
8 0.26724937 h
9 -1.13132950 i
10 0.31408454 j
11 0.64873521 k
12 0.33610366 l
13 -1.82775097 m
14 -0.18508915 n
15 0.59403868 o
16 0.21510182 p
17 0.81497239 q
18 0.71799874 r
19 0.22732659 s
20 0.46474950 t
identical(df1,df2)
[1] FALSE

Example

 Live Demo

df1<-data.frame(x1=rnorm(20),x2=letters[1:20])
df1

Output

   x1    x2
1 0.34718972 a
2 -0.26756669 b
3 0.52007492 c
4 0.06377162 d
5 -0.11679032 e
6 -0.23173790 f
7 -0.53550405 g
8 1.07821494 h
9 -0.02160426 i
10 -0.12502472 j
11 0.27371128 k
12 -0.28931486 l
13 -0.36478099 m
14 0.79732361 n
15 1.24983019 o
16 1.15472390 p
17 0.30553105 q
18 -0.20471787 r
19 -2.28367371 s
20 1.30177853 t
df2<-data.frame(x1=rnorm(20),x2=letters[1:20])
identical(df1,df2)
[1] FALSE
df3<-data.frame(x1=LETTERS[1:20],x2=1:20)
df3
x1 x2
1 A 1
2 B 2
3 C 3
4 D 4
5 E 5
6 F 6
7 G 7
8 H 8
9 I 9
10 J 10
11 K 11
12 L 12
13 M 13
14 N 14
15 O 15
16 P 16
17 Q 17
18 R 18
19 S 19
20 T 20
df4<-data.frame(y1=LETTERS[1:20],y2=1:20)
identical(df3,df4)
[1] FALSE
df5<-data.frame(x1=LETTERS[1:20],x2=1:20)
identical(df3,df5)
[1] TRUE
raja
Published on 21-Aug-2020 10:41:55

Advertisements