如何删除R中的重复行(附实例)

3,135 阅读1分钟

你可以使用以下两种方法之一来删除R中数据框中的重复行:

方法1:使用Base R

#remove duplicate rows across entire data frame
df[!duplicated(df), ]

#remove duplicate rows across specific columns of data frame
df[!duplicated(df[c('var1')]), ]

方法2:使用dplyr

#remove duplicate rows across entire data frame 
df %>%
  distinct(.keep_all = TRUE)

#remove duplicate rows across specific columns of data frame
df %>%
  distinct(var1, .keep_all = TRUE)

下面的例子展示了如何在实践中使用这种语法,其数据框如下:

#define data frame
df <- data.frame(team=c('A', 'A', 'A', 'B', 'B', 'B'),
                 position=c('Guard', 'Guard', 'Forward', 'Guard', 'Center', 'Center'))

#view data frame
df

  team position
1    A    Guard
2    A    Guard
3    A  Forward
4    B    Guard
5    B   Center
6    B   Center

例1:使用Base R删除重复的行

下面的代码显示了如何使用基础R的函数从数据框中删除重复的行:

#remove duplicate rows from data frame
df[!duplicated(df), ]

  team position
1    A    Guard
3    A  Forward
4    B    Guard
5    B   Center

下面的代码显示了如何使用基础R从数据框的特定列中删除重复的行:

#remove rows where there are duplicates in the 'team' column
df[!duplicated(df[c('team')]), ]

  team position
1    A    Guard
4    B    Guard

例2:使用dplyr删除重复的行

下面的代码显示了如何使用dplyr包中的distinct ()函数从数据框中删除重复的行:

library(dplyr)

#remove duplicate rows from data frame
df %>%
  distinct(.keep_all = TRUE)

  team position
1    A    Guard
2    A  Forward
3    B    Guard
4    B   Center

请注意,.keep_all参数告诉R保留原始数据框中的所有列。

下面的代码显示了如何使用distinct()函数从一个数据框架的特定列中删除重复的行:

library(dplyr)

#remove duplicate rows from data frame
df %>%
  distinct(team, .keep_all = TRUE)

  team position
1    A    Guard
2    B    Guard

其他资源

下面的教程解释了如何在R中执行其他常用函数:

如何在R中根据条件删除行
如何在R中删除一个特定列中有NA的行