如何在R数据框中找到唯一的行?

R数据帧中的唯一行意味着在整个数据帧中,该行中的所有元素都不会以相同的组合重复。用简单的话说,如果我们有一个名为df的数据帧,其中包含3列和5行,则特定行中的所有值都不会在其他任何行中重复。当我们在数据集中有很多重复的行时,可能需要搜索这种类型的行。为此,我们可以使用dplyr包的group_by_all函数,如以下示例所示。

例1

考虑以下数据帧-

> x1<-rpois(20,1)
> x2<-rpois(20,1)
> x3<-rpois(20,1)
> df1<-data.frame(x1,x2,x3)
> df1
输出结果
   x1 x2 x3
1  1  0  2
2  2  1  2
3  1  0  1
4  0  1  0
5  0  0  1
6  1  1  1
7  0  0  0
8  0  1  1
9  0  0  0
10 1  0  1
11 2  2  2
12 1  2  1
13 2  0  2
14 0  1  0
15 0  1  1
16 1  0  1
17 0  0  2
18 1  1  1
19 4  2  0
20 2  2  0

加载dplyr软件包并在df1中查找唯一的行-

> library(dplyr)
> df1%>%group_by_all%>%count
# A tibble: 14 x 4
# Groups: x1, x2, x3 [14]
输出结果
    x1    x2    x3    n
  <int> <int> <int> <int>
1   0     0     0     2
2   0     0     1     1
3   0     0     2     1
4   0     1     0     2
5   0     1     1     2
6   1     0     1     3
7   1     0     2     1
8   1     1     1     2
9   1     2     1     1
10  2     0     2     1
11  2     1     2     1
12  2     2     0     1
13  2     2     2     1
14  4     2     0     1

例2

> y1<-sample(c("Yes","No"),20,replace=TRUE)
> y2<-sample(c("Yes","No"),20,replace=TRUE)
> df2<-data.frame(y1,y2)
> df2
输出结果
  y1   y2
1 No   Yes
2 No   Yes
3 No   No
4 Yes  No
5 No   No
6 Yes  Yes
7 No   No
8 Yes  Yes
9 No   No
10 No  No
11 No  Yes
12 No  Yes
13 Yes No
14 No  Yes
15 No  No
16 Yes No
17 Yes No
18 No  Yes
19 No  Yes
20 Yes No

在df2中查找唯一的行-

> df2%>%group_by_all%>%count
# A tibble: 4 x 3
# Groups: y1, y2 [4]
输出结果
   y1      y2     n
  <int>  <int> <int>
1  No     No     6
2  No     Yes    7
3  Yes    No     5
4  Yes    Yes    2

范例3

> z1<-sample(1:4,20,replace=TRUE)
> z2<-sample(1:4,20,replace=TRUE)
> df3<-data.frame(z1,z2)
> df3
输出结果
  z1 z2
1  1 4
2  2 3
3  1 4
4  1 3
5  4 3
6  2 3
7  3 2
8  1 3
9  1 3
10 1 4
11 4 1
12 2 1
13 4 4
14 4 4
15 3 3
16 4 2
17 4 1
18 4 2
19 2 1
20 1 3

在df3中查找唯一的行-

> df3%>%group_by_all%>%count
# A tibble: 10 x 3
# Groups: z1, z2 [10]
z1 z2 n
输出结果
  <int> <int> <int>
1   1     3     4
2   1     4     3
3   2     1     2
4   2     3     2
5   3     2     1
6   3     3     1
7   4     1     2
8   4     2     2
9   4     3     1
10  4     4     2