我读了一个CSV文件到R data.frame。有些行在其中一列中有相同的元素。我想删除该列中重复的行。例如:
platform_external_dbus 202 16 google 1
platform_external_dbus 202 16 space-ghost.verbum 1
platform_external_dbus 202 16 localhost 1
platform_external_dbus 202 16 users.sourceforge 8
platform_external_dbus 202 16 hughsie 1
我只需要其中一行,因为其他的在第一列中有相同的数据。
删除数据帧的重复行
library(dplyr)
mydata <- mtcars
# Remove duplicate rows of the dataframe
distinct(mydata)
在这个数据集中,没有一个重复的行,所以它返回与mydata中相同的行数。
基于一个变量删除重复行
library(dplyr)
mydata <- mtcars
# Remove duplicate rows of the dataframe using carb variable
distinct(mydata,carb, .keep_all= TRUE)
.keep_all函数用于保留输出数据帧中的所有其他变量。
基于多个变量删除重复行
library(dplyr)
mydata <- mtcars
# Remove duplicate rows of the dataframe using cyl and vs variables
distinct(mydata, cyl,vs, .keep_all= TRUE)
.keep_all函数用于保留输出数据帧中的所有其他变量。
(来源:http://www.datasciencemadesimple.com/remove-duplicate-rows-r-using-dplyr-distinct-function/)
一般的答案可以是
例如:
df <- data.frame(rbind(c(2,9,6),c(4,6,7),c(4,6,7),c(4,6,7),c(2,9,6))))
new_df <- df[-which(duplicated(df)), ]
输出:
X1 X2 X3
1 2 9 6
2 4 6 7
删除数据帧的重复行
library(dplyr)
mydata <- mtcars
# Remove duplicate rows of the dataframe
distinct(mydata)
在这个数据集中,没有一个重复的行,所以它返回与mydata中相同的行数。
基于一个变量删除重复行
library(dplyr)
mydata <- mtcars
# Remove duplicate rows of the dataframe using carb variable
distinct(mydata,carb, .keep_all= TRUE)
.keep_all函数用于保留输出数据帧中的所有其他变量。
基于多个变量删除重复行
library(dplyr)
mydata <- mtcars
# Remove duplicate rows of the dataframe using cyl and vs variables
distinct(mydata, cyl,vs, .keep_all= TRUE)
.keep_all函数用于保留输出数据帧中的所有其他变量。
(来源:http://www.datasciencemadesimple.com/remove-duplicate-rows-r-using-dplyr-distinct-function/)