删除R中具有聚合组的重复项
以下是我的数据示例:删除R中具有聚合组的重复项,r,dataframe,dplyr,data.table,R,Dataframe,Dplyr,Data.table,以下是我的数据示例: kod <- structure(list(ID_WORKES = c(28029571L, 28029571L, 28029571L, 28029571L, 28029571L, 28029571L, 28029571L, 28029571L, 28029571L ), TABL_NOM = c(9716L, 9716L, 9716L, 9716L, 9716L, 9716L, 9716L, 9716L, 9716L), NAME = structure(c(1
kod <- structure(list(ID_WORKES = c(28029571L, 28029571L, 28029571L,
28029571L, 28029571L, 28029571L, 28029571L, 28029571L, 28029571L
), TABL_NOM = c(9716L, 9716L, 9716L, 9716L, 9716L, 9716L, 9716L,
9716L, 9716L), NAME = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = "Dim", class = "factor"), ID_SP_NAR = c(20L,
20L, 20L, 30L, 30L, 30L, 30L, 30L, 30L), KOD_DOR = c(28L, 28L,
28L, 28L, 28L, 28L, 28L, 28L, 28L), KOD_DEPO = c(9167L, 9167L,
9167L, 9167L, 9167L, 9167L, 9167L, 9167L, 9167L), COLUMN_MASH = c(13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L), prop_violations = c(0.00561797752808989,
0.00293255131964809, 0.00495049504950495, 0.00215982721382289,
0.0120481927710843, 0.00561797752808989, 0.00293255131964809,
0.00591715976331361, 0.00495049504950495), mash_score = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), row.names = c(NA, -9L), class = "data.frame")
W
但还有一件事:如果对于ID_SP_NAR的prop_违规中的一些重复值,mash_分数的值大于0,那么最后一个mash_分数的值大于0
比如说
ID_WORKES TABL_NOM NAME ID_SP_NAR KOD_DOR KOD_DEPO COLUMN_MASH prop_violations mash_score
1 28029571 9716 Dim 30 28 9167 13 0,0056 0
2 28029571 9716 Dim 30 28 9167 13 0,012048193 0
3 28029571 9716 Dim 30 28 9167 13 0,005617978 0
4 28029571 9716 Dim 30 28 9167 13 0,002932551 1
5 28029571 9716 Dim 30 28 9167 13 0,00591716 0
6 28029571 9716 Dim 30 28 9167 13 0,004950495 0
在这种情况下,ID_SP_NAR=30的prop_违规只会留下值0002932551,导致mash_得分>0
如何达到此条件?以下是使用
tidyverse
软件包的解决方案:
kod %>%
group_by(ID_WORKES, TABL_NOM, NAME, KOD_DOR, KOD_DEPO, ID_SP_NAR) %>%
summarise(prop_violations = if (all(mash_score == 0)) mean(prop_violations) else last(prop_violations[mash_score > 0]))
如果特定组的所有
mash_得分
均等于零,则返回平均值(使用mean
)。如果至少有一个mash_得分
大于零,则返回mash_得分>0
的prop_违规
的最后一个值(使用dplyr::last
)。使用数据的选项。表
:
setDT(kod)
kod[, {
if(any(mash_score)>0) {
i <- which(mash_score>0)[1L]
.(prop_violations=prop_violations[i], mash_score=mash_score[i])
} else
.(prop_violations=mean(prop_violations), mash_score=mash_score[1L])
},
.(ID_WORKES, TABL_NOM, NAME, KOD_DOR, KOD_DEPO, ID_SP_NAR)]
数据:
kod
setDT(kod)
kod[, {
if(any(mash_score)>0) {
i <- which(mash_score>0)[1L]
.(prop_violations=prop_violations[i], mash_score=mash_score[i])
} else
.(prop_violations=mean(prop_violations), mash_score=mash_score[1L])
},
.(ID_WORKES, TABL_NOM, NAME, KOD_DOR, KOD_DEPO, ID_SP_NAR)]
ID_WORKES TABL_NOM NAME KOD_DOR KOD_DEPO ID_SP_NAR prop_violations mash_score
1: 28029571 9716 Dim 28 9167 20 0.004500341 0
2: 28029571 9716 Dim 28 9167 30 0.002932551 1
kod <- structure(list(ID_WORKES = c(28029571L, 28029571L, 28029571L,
28029571L, 28029571L, 28029571L, 28029571L, 28029571L, 28029571L
), TABL_NOM = c(9716L, 9716L, 9716L, 9716L, 9716L, 9716L, 9716L,
9716L, 9716L), NAME = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = "Dim", class = "factor"), ID_SP_NAR = c(20L,
20L, 20L, 30L, 30L, 30L, 30L, 30L, 30L), KOD_DOR = c(28L, 28L,
28L, 28L, 28L, 28L, 28L, 28L, 28L), KOD_DEPO = c(9167L, 9167L,
9167L, 9167L, 9167L, 9167L, 9167L, 9167L, 9167L), COLUMN_MASH = c(13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L), prop_violations = c(0.00561797752808989,
0.00293255131964809, 0.00495049504950495, 0.00215982721382289,
0.0120481927710843, 0.00561797752808989, 0.00293255131964809,
0.00591715976331361, 0.00495049504950495), mash_score = c(0L,
0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L)), row.names = c(NA, -9L), class = "data.frame")