[英]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 这是可复制的数据集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. 表示此处在数据帧中,如果有2-3行且第一列的值相同,则仅应保留第一行,其余的行应删除。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.