删除R中的重复行? [英] Delete duplicate rows in R?

查看:216
本文介绍了删除R中的重复行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,有重复的观察,如何保持唯一的观察?

  ID日期类型
1 201301 A
2 201308 B
4 201303 R
1 201301 A
3 201305 C
2 201308 B

我想要的是:

  ID日期类型
1 201301 A
2 201308 B
4 201303 R
3 201305 C

我尝试了独特的&复制功能。但是它没有起作用。

  dataset [which(dataset $ ID%in%unique(dataset $ ID))] #将保留所有行

数据集[!重复(数据集$ ID),]#只能保留ID = 3,4,如下
ID日期类型
4 201303 R
3 201305 C

如何获取R中的目标数据集? p>

解决方案

  unique数据集)

 数据集[!duplicateated(dataset),] 

>

(将答案从评论复制到正确的答案)。


I have a dataset, there are duplicate observations, how to keep the unique observation?

ID   Date   Type
1   201301  A
2   201308  B
4   201303  R
1   201301  A
3   201305  C
2   201308  B

What I want is:

ID   Date   Type
1   201301  A
2   201308  B
4   201303  R
3   201305  C

I tried the unique & duplicated function. But it didn't work.

dataset[which(dataset$ID %in% unique(dataset$ID)),]  # will keep all the row

dataset[!duplicated(dataset$ID),] #will only keep the ID=3,4,as follows
ID   Date   Type
4   201303  R
3   201305  C

How can I get the target dataset in R?

解决方案

Either

unique(dataset)

or

dataset[!duplicated(dataset),]

will work.

(Copying the answer from the comments into a proper answer).

这篇关于删除R中的重复行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆