Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何基于三列删除重复项,但使用R保留特定列中数字最高的行?_R_Dataframe_Duplicates - Fatal编程技术网

如何基于三列删除重复项,但使用R保留特定列中数字最高的行?

如何基于三列删除重复项,但使用R保留特定列中数字最高的行?,r,dataframe,duplicates,R,Dataframe,Duplicates,我的数据集如下所示: Unique Id|Class Id|Version Id 501 1 1 602 3 1 602 3 1 405 2 1 305 2 3 305 2 2 305 1

我的数据集如下所示:

 Unique Id|Class Id|Version Id
       501        1          1
       602        3          1
       602        3          1
       405        2          1
       305        2          3
       305        2          2
       305        1          1
       305        2          1
       509        1          1
       501        2          1
       501        3          1
       501        3          2
       602        2          1
       602        1          1
       405        1          1
如果要运行脚本,其余条目应为:

 Unique Id|Class Id|Version Id
       501        1          1
       602        3          1
       405        2          1
       305        2          3
       305        1          1
       509        1          1
       501        2          1
       501        3          2
       602        2          1
       602        1          1
       405        1          1
请注意,选择了唯一id:501类id:3和版本id:2,因为它具有最高的版本id。请注意,删除了唯一id:602类id:3和版本id:1,因为它自始至终完全相同

基本上,我希望脚本删除基于三列的所有重复项,并保留具有最高版本id的行。

我们可以在UniqueID列上使用rleid,并在根据“唯一id”和类id上的rleid分组后执行slice_max

-输出

# A tibble: 11 x 3
#   `Unique Id` `Class Id` `Version Id`
#         <int>      <int>        <int>
# 1         501          1            1
# 2         602          3            1
# 3         405          2            1
# 4         305          1            1
# 5         305          2            3
# 6         509          1            1
# 7         501          2            1
# 8         501          3            2
# 9         602          1            1
#10         602          2            1
#11         405          1            1
或者使用base R

数据 我们可以在UniqueID列上使用rleid,并在通过“Unique Id”和类Id上的rleid进行分组后执行slice_max

-输出

# A tibble: 11 x 3
#   `Unique Id` `Class Id` `Version Id`
#         <int>      <int>        <int>
# 1         501          1            1
# 2         602          3            1
# 3         405          2            1
# 4         305          1            1
# 5         305          2            3
# 6         509          1            1
# 7         501          2            1
# 8         501          3            2
# 9         602          1            1
#10         602          2            1
#11         405          1            1
或者使用base R

数据
如果顺序无关紧要,那么我们可以对数据重新排序,以便更高版本的ID位于顶部,然后删除重复的条目

df <- df[order(df[,1], df[,2], -df[,3]),]
df <- df[!duplicated(df[,-3]),]

df
       Unique Id Class Id Version Id
7        305        1          1
5        305        2          3
15       405        1          1
4        405        2          1
1        501        1          1
10       501        2          1
12       501        3          2
9        509        1          1
14       602        1          1
13       602        2          1
2        602        3          1

如果顺序无关紧要,那么我们可以对数据重新排序,以便更高版本的ID位于顶部,然后删除重复的条目

df <- df[order(df[,1], df[,2], -df[,3]),]
df <- df[!duplicated(df[,-3]),]

df
       Unique Id Class Id Version Id
7        305        1          1
5        305        2          3
15       405        1          1
4        405        2          1
1        501        1          1
10       501        2          1
12       501        3          2
9        509        1          1
14       602        1          1
13       602        2          1
2        602        3          1

这是否考虑到了其他两列,如class和version?@user35131我想现在的解决方案完全符合您的预期,这正是我想要的!!完美的谢谢。这是否考虑了其他两列,如class和version?@user35131我认为现在的解决方案完全符合您的预期,这正是我所寻找的!!完美的谢谢。如果顺序很重要,它也在rownamesdf中。如果顺序很重要,它也在rownamesdf中。
data <- structure(list(`Unique Id` = c(501L, 602L, 602L, 405L, 305L, 
305L, 305L, 305L, 509L, 501L, 501L, 501L, 602L, 602L, 405L), 
    `Class Id` = c(1L, 3L, 3L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 3L, 
    3L, 2L, 1L, 1L), `Version Id` = c(1L, 1L, 1L, 1L, 3L, 2L, 
    1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L)), class = "data.frame", 
    row.names = c(NA, 
-15L))
df <- df[order(df[,1], df[,2], -df[,3]),]
df <- df[!duplicated(df[,-3]),]

df
       Unique Id Class Id Version Id
7        305        1          1
5        305        2          3
15       405        1          1
4        405        2          1
1        501        1          1
10       501        2          1
12       501        3          2
9        509        1          1
14       602        1          1
13       602        2          1
2        602        3          1