Delete duplicate rows in R?

≯℡__Kan透↙ 提交于 2019-12-20 06:10:44

问题


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?


回答1:


Either

unique(dataset)

or

dataset[!duplicated(dataset),]

will work.

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



来源:https://stackoverflow.com/questions/18308320/delete-duplicate-rows-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!