R语言 使用Dplyr删除重复行

R语言 使用Dplyr删除重复行

在这篇文章中,我们将使用Dplyr包在R编程语言中删除重复的行。

方法1: distinct()

该函数用于移除数据框中的重复行,并获得唯一的数据

语法

distinct(dataframe)

我们也可以根据数据框架中的多列/变量来移除重复的行

语法

distinct(dataframe,column1,column2,. , column n)

使用中的数据集

使用Dplyr删除R语言中的重复行

例1: R程序从数据框中移除重复的行

# load the package
library(dplyr)
  
# create dataframe with three columns
# named id,name and address
data1=data.frame(id=c(1,2,3,4,5,6,7,1,4,2),
                   
                 name=c('sravan','ojaswi','bobby',
                        'gnanesh','rohith','pinkey',
                        'dhanush','sravan','gnanesh',
                        'ojaswi'),
                   
                 address=c('hyd','hyd','ponnur','tenali',
                           'vijayawada','vijayawada','guntur',
                           'hyd','tenali','hyd'))
  
# remove duplicate rows
print(distinct(data1))
R

输出

使用Dplyr删除R语言中的重复行

例2: 基于单列删除重复的行

# load the package
library(dplyr)
  
# create dataframe with three columns 
# named id,name and address
data1=data.frame(id=c(1,2,3,4,5,6,7,1,4,2),
                   
                 name=c('sravan','ojaswi','bobby',
                        'gnanesh','rohith','pinkey',
                        'dhanush','sravan','gnanesh',
                        'ojaswi'),
                   
                 address=c('hyd','hyd','ponnur','tenali',
                           'vijayawada','vijayawada','guntur',
                           'hyd','tenali','hyd'))
  
# remove duplicate rows based on name 
# column
print(distinct(data1,name))
R

输出

使用Dplyr删除R语言中的重复行

例3: 基于多列删除重复的行

# load the package
library(dplyr)
  
# create dataframe with three columns 
# named id,name and address
data1=data.frame(id=c(1,2,3,4,5,6,7,1,4,2),
                   
                 name=c('sravan','ojaswi','bobby',
                        'gnanesh','rohith','pinkey',
                        'dhanush','sravan','gnanesh',
                        'ojaswi'),
                   
                 address=c('hyd','hyd','ponnur','tenali',
                           'vijayawada','vijayawada','guntur',
                           'hyd','tenali','hyd'))
  
# remove duplicate rows based on 
# name and address columns
print(distinct(data1,address,name))
R

输出

使用Dplyr删除R语言中的重复行

方法2:使用 duplicated() 函数

duplicated()函数将返回重复的行,而!Doubleicated()函数将返回唯一的行。

语法

dataframe[! duplicated(dataframe$column_name), ]

这里,dataframe是输入的数据框架,column_name是数据框架中的列,基于该列,重复的数据被移除。

例子: R程序基于特定的列删除重复的数据

# load the package
library(dplyr)
  
# create dataframe with three columns
# named id,name and address
data1=data.frame(id=c(1,2,3,4,5,6,7,1,4,2),
                   
                 name=c('sravan','ojaswi','bobby',
                        'gnanesh','rohith','pinkey',
                        'dhanush','sravan','gnanesh',
                        'ojaswi'),
                   
                 address=c('hyd','hyd','ponnur','tenali',
                           'vijayawada','vijayawada','guntur',
                           'hyd','tenali','hyd'))
  
# remove duplicate rows using duplicated()
# function based on name column
print(data1[!duplicated(data1name), ] )
print("=====================")
  
# remove duplicate rows using duplicated()
# function based on id column
print(data1[!duplicated(data1id), ] )
print("=====================")
  
# remove duplicate rows using duplicated()
# function based on address column
print(data1[!duplicated(data1$address), ] )
print("=====================")
R

输出

使用Dplyr删除R语言中的重复行

方法3:使用unique()函数

unique()函数用于通过返回唯一的数据来删除重复的行。

语法

unique(dataframe)

要从列中获得唯一的数据,请将列的名称与数据框架的名称一起传递。

语法

unique(dataframe$column_name)

其中,dataframe是输入数据框架,column_name是数据框架中的列。

例1: 使用unique()函数去除重复的R程序

# load the package
library(dplyr)
  
# create dataframe with three columns
# named id,name and address
data1=data.frame(id=c(1,2,3,4,5,6,7,1,4,2),
                   
                 name=c('sravan','ojaswi','bobby',
                        'gnanesh','rohith','pinkey',
                        'dhanush','sravan','gnanesh',
                        'ojaswi'),
                   
                 address=c('hyd','hyd','ponnur','tenali',
                           'vijayawada','vijayawada','guntur',
                           'hyd','tenali','hyd'))
  
# get unique data from the dataframe
print(unique(data1))
R

输出

使用Dplyr删除R语言中的重复行

例2: R程序删除特定列中的重复内容

# load the package
library(dplyr)
  
# create dataframe with three columns
# named id,name and address
data1=data.frame(id=c(1,2,3,4,5,6,7,1,4,2),
                   
                 name=c('sravan','ojaswi','bobby',
                        'gnanesh','rohith','pinkey',
                        'dhanush','sravan','gnanesh',
                        'ojaswi'),
                   
                 address=c('hyd','hyd','ponnur','tenali',
                           'vijayawada','vijayawada','guntur',
                           'hyd','tenali','hyd'))
  
# get unique data from the dataframe
# in id column
print(unique(data1id))
  
# get unique data from the dataframe 
# in name  column
print(unique(data1name))
  
# get unique data from the dataframe 
# in address column
print(unique(data1$address))
R

输出

使用Dplyr删除R语言中的重复行

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册