简体   繁体   中英

How to get unique rows from the Index column in R data frame

Here is a reproducible dataset https://drive.google.com/file/d/0B3cafW7J7xSfNmhJQzh3SF9VYms/edit?usp=sharing

I want to delete all the rows which have a same value as the first column of accident index. means that here in data frame if there are 2-3 rows with the first column value same then only 1st row should be kept and others deleted.

I tried the following but it didn't work.

v2<-v1[which(v1$i..Accident_Index==unique(v1$i..Accident_Index))]

Please help..thanks

You can try:

v2 <- v1[!duplicated(v1$i..Accident_Index), ]

To demonstrate this does answer the question:

v1 <- data.frame(i..Accident_Index=rep(1:3, each=2), b=letters[1:6])
v1[!duplicated(v1$i..Accident_Index), ]

Produces:

  i..Accident_Index b
1                 1 a
3                 2 c
5                 3 e

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM