R,有条件地删除重复行 [英] R, conditionally remove duplicate rows

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

问题描述

我在 R 中有一个数据框,其中包含 ID.A、ID.B 和 DISTANCE 列,其中距离表示 ID.A 和 ID.B 之间的距离.对于 ID.A 的每个值 (1->n),可能有多个 ID.B 和 DISTANCE 值(即 ID.A 中可能有多个重复行,例如所有值 4,每个都有不同的 ID.B和那一行的距离).

I have a dataframe in R containing the columns ID.A, ID.B and DISTANCE, where distance represents the distance between ID.A and ID.B. For each value (1->n) of ID.A, there may be multiple values of ID.B and DISTANCE (i.e. there may be multiple duplicate rows in ID.A e.g. all of value 4 which each has a different ID.B and distance in that row).

我希望能够删除 ID.A 重复的行,但以距离值为条件,以便我为每个 ID.A 记录留下最小的距离值.

I would like to be able to remove rows where ID.A is duplicated, but conditional upon the distance value such that I am left with the smallest distance values for each ID.A record.

希望这是有道理的?

在此先感谢

编辑

希望一个例子比我的文字更有用.在这里,我想删除 ID.A = 3 的第二行和第三行:

Hopefully an example will prove more useful than my text. Here I would like to remove the second and third rows where ID.A = 3:

myDF <- read.table(text="ID.A ID.B DISTANCE
  1 3 1
  2 6 8
  3 2 0.4
  3 3 1
  3 8 5
  4 8  7
  5 2 11", header = TRUE)

推荐答案

一种可能:

myDF <- myDF[order(myDF$ID.A, myDF$DISTANCE), ] 

newdata <- myDF[which(!duplicated(myDF$ID.A)),]

这给出了:

    ID.A ID.B DISTANCE
1    1    3      1.0
2    2    6      8.0
5    3    2      0.4
6    4    8      7.0
7    5    2     11.0

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

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