根据规则删除多个列和行中的重复项

时间:2018-08-07 07:50:55

标签: r duplicates conditional-statements

假设我有以下数据:

dt <- data.frame(id=c(1,1,2,2,3,3,3,4,5,5,5,5,6,7,7),
             rk=c("a","a","b","b","c","y","c","d","e","y","e","e","f","g","h"),
             .id=c("df1", "df9", "df5", "df16", "df2", "df11", "df11", "df4", "df9", "df4", "df6", "df3", "df16", "df2", "df9"))

所以我的数据如下:

id   rk  .id
1    a   df1
1    a   df9
2    b   df5
2    b  df16
3    c   df2
3    y  df11
3    c  df11
4    d   df4
5    e   df9
5    y   df4
5    e   df6
5    e   df3
6    f  df16
7    g   df2
7    h   df9

但是我只希望每对 id rk 每行一个行。 因此,在示例中,id = 5可以有两行:一行带有rk = e,另一行带有rk = y。

要找到要保留的右行,请查看 .id 列。在这里,我按以下顺序确定优先级:

df2,df9,df1,df5,df4,df6,df15,df17,df16,df14,df8,df11,df3,df7,df12,df13,df10

因此,我将始终在.id = df9的行上保留.id = df2的行。同样,我将始终在.id = df14的行中保留.id = df14的行。

请注意,该顺序不是按时间顺序排列的。

回到我的示例数据,这就是我想要得到的结果:

id   rk  .id
1    a   df9
2    b   df5
3    c   df2
3    y  df11
4    d   df4
5    e   df9
5    y   df4
6    f  df16
7    g   df2
7    h   df9

我的数据集很大,所以我希望你们中的一些人可以帮助我编写一些使这变得容易的代码。

2 个答案:

答案 0 :(得分:6)

使用dplyr,我们可以group_by idrk,并用match获得.id中的第一个new_order。 / p>

library(dplyr)
dt %>%
  group_by(id, rk) %>%
  summarise(.id = .id[which.min(match(.id, new_order))])

#   id rk    .id  
#   <dbl> <fct> <fct>
# 1  1.00 a     df9  
# 2  2.00 b     df5  
# 3  3.00 c     df2  
# 4  3.00 y     df11 
# 5  4.00 d     df4  
# 6  5.00 e     df9  
# 7  5.00 y     df4  
# 8  6.00 f     df16 
# 9  7.00 g     df2  
#10  7.00 h     df9 

等效的基本R aggregate选项是

aggregate(.id~id+rk, dt, function(x) x[which.min(match(x, new_order))]) 

如果要保留其他一些列,可以使用filter代替summarise

dt %>%
 group_by(id, rk) %>%
 filter(.id == .id[which.min(match(.id, new_order))])

与之等效的ave选项是

dt[with(dt, .id ==  ave(.id, id, rk, FUN = function(x) 
                    x[which.min(match(x, new_order))])), ]

其中

new_order <- c("df2", "df9", "df1", "df5", "df4", "df6", "df15", "df17", "df16",
           "df14", "df6", "df8", "df11", "df3", "df7", "df12", "df13", "df10")

答案 1 :(得分:0)

我会用data.table这样来做。看起来有点长,但相当直观。

library(data.table)

# Load datasets
dt <- data.frame(id=c(1,1,2,2,3,3,3,4,5,5,5,5,6,7,7),
                 rk=c("a","a","b","b","c","y","c","d","e","y","e","e","f","g","h"),
                 .id=c("df1", "df9", "df5", "df16", "df2", "df11", "df11", "df4", "df9", "df4", "df6", "df3", "df16", "df2", "df9"))


Priority_List <- c("df2", "df9", "df1", "df5", "df4", "df6", "df15", "df17", "df16",
                   "df14", "df6", "df8", "df11", "df3", "df7", "df12", "df13", "df10")

# Create a data table called priority list with the priority rank
Priority_List <- data.table(.id = Priority_List , Priority = 1:length(Priority_List))

# Convert your parent data.frame into data.table
dt <- data.table(dt)

# Merge the Priority List with dt based on .id
dt <- merge(dt,Priority_List, by =c(".id"), all.x = TRUE)

# Find the minimum priority for each id and rk
dt <- dt[, Min_Priority := min(Priority), by = c("id", "rk")]

# Filter when Priority is equal to the Min_Priority for a particular id, rk
dt <- dt[Min_Priority == Priority]

# Take unique in case there are duplicate rows.
dt <- unique(dt)

# Remove unwanted columns and order based on id and rk
dt <- dt[,.(id, rk, .id)][order(id, rk)]

希望这会有所帮助。