简体   繁体   English

如何从R数据框中的索引列获取唯一行

[英]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.

相关问题 如何从带有索引列的数据框中提取唯一行? - How to extract unique rows from a data frame with an index column? 在使用 unique() function 之后,如何从具有重复案例的 data.frame 中获取行的索引? - How can I get the index of the rows, from a data.frame with repeated cases, after using the unique() function? 如何从R中的面板数据框中删除具有唯一ID的行? - How to delete rows with a unique ID from a panel data frame in R? 如何从数据框中删除与 R 中单独数据框中唯一值相等的所有行? - How do I remove all rows from data frame that equal unique value in seperate data frame in R? 当列与R中的其他数据框列匹配时,如何从数据框中删除行 - How to remove rows from a data frame when the column matches with a different data frame column in R 如何根据 R 中数据框中的某些列和行信息删除唯一行 - How do I remove unique rows based on certain column and row information in a data frame in R 从数据框中的 label 获取列索引 - Get column index from label in a data frame 从R中的数据帧中提取唯一的组合行 - extracting unique combination rows from a data frame in R 是否可以在 R 中获取未选择的数据框行的索引? - Is that possible to get the index of unselected rows of data frame in R? R - 如何从数据框中的单列和单行获取字符串 - R - How to get at a string from a single column and row in a data frame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM