根据订单删除重复项 [英] Remove duplicates based on order
本文介绍了根据订单删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在R中,我希望删除每个a
之后的前两个b
和c
之后的所有实例(请注意编号).
我有以下内容:
In R, I'm looking to remove any instances after the first two b
and c
after each a
(please note the numbering).
I've got the following:
1 a
2 b
3 c
4 a
5 b
6 c
7 a
8 b
9 c
10 b
11 c
12 a
13 b
14 c
15 c
我希望将其减少为:
1 a
2 b
3 c
4 a
5 b
6 c
7 a
8 b
9 c
12 a
13 b
14 c
如果可能,我正在尝试在dplyr
管道中执行此操作.
有什么想法吗?
I'm trying to do this within a dplyr
pipe if possible.
Any ideas?
推荐答案
一种可能的解决方案:
df = read.table(text="1 a
2 b
3 c
4 a
5 b
6 c
7 a
8 b
9 c
10 b
11 c
12 a
13 b
14 c
15 c",header=F)
library(dplyr)
df %>% mutate(x=cumsum(V2=='a')) %>%
group_by(x) %>%
filter(!duplicated(V2)) %>%
ungroup() %>%
select(-x)
输出:
# A tibble: 12 x 2
V1 V2
<int> <fctr>
1 1 a
2 2 b
3 3 c
4 4 a
5 5 b
6 6 c
7 7 a
8 8 b
9 9 c
10 12 a
11 13 b
12 14 c
请注意,这每次遇到a
后都会删除所有重复的元素.如果只想删除重复的b
和c
,请考虑:filter(!(duplicated(V2) & (V2=='b' | V2=='c')))
Note that this removes all duplicated elements every time after an a
is encountered. If you only want to remove duplicated b
's and c
's, consider : filter(!(duplicated(V2) & (V2=='b' | V2=='c')))
这篇关于根据订单删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文