R语言 寻找R数据框架中的唯一行
在R数据框中,唯一的行意味着该行中的元素在整个数据框中没有相同的组合被复制。简单来说,如果我们有一个叫做df的数据框,有四列五行,我们可以假设某一行的值没有一个是在其他每一行中复制的。
如果我们的数据集合中有许多冗余的行,我们可以需要寻找某些类型的行。我们可以使用dplyr包的group_by_all函数来实现这一点。它将对所有的冗余行进行分组,并返回带有计数的唯一行。
例1 :
library("dplyr")
df = data.frame(x = as.integer(c(1, 1, 2, 2, 3, 4, 4)),
y = as.integer(c(1, 1, 2, 2, 3, 4, 4)))
print("dataset is ")
print(df)
ans = df%>%group_by_all%>%count
print("unique rows with count are")
print(ans)
输出
例2 :
library("dplyr")
df = data.frame(x = as.integer( c(10,10,20,20,30,40,40) ),
y = c("rahul", "rahul", "mohan","mohan", "rohit", "rohan", "rohan"))
print("dataset is ")
print(df)
ans = df%>%group_by_all%>%count
print("unique rows with count are")
print(ans)
输出