从一列序列中选择特定序列,其间的距离最大

Selecting specific sequences from a column of sequences, with a maximum distance in between

我有如下数据:

data <- as.data.frame(c("[0;20;1;2;3;4;5;6;7;22;8;9;10;11;12;13;14;23;17;24;18;25;15;26;16;19]", 
                        "[0;21;16;27;15;28;8;9;10;11;12;13;14;29;1;2;3;4;5;6;7;30;18;31;17;19]", 
                        "[0;20;15;22;16;23;18;24;17;25;1;2;3;4;5;6;7;26;8;9;10;11;12;13;14;19]", 
                        "[0;20;8;9;10;11;12;13;14;22;1;2;3;4;5;6;7;23;15;24;16;25;17;26;18;19]", 
                        "[0;21;18;27;17;28;15;29;16;30;8;9;10;11;12;13;14;31;1;2;3;4;5;6;7;19]", 
                        "[0;20;1;2;3;4;5;6;7;22;8;9;10;11;12;13;14;23;15;24;16;25;17;26;18;19]", 
                        "[0;21;1;2;3;4;5;6;7;27;8;9;10;11;12;13;14;28;17;29;18;30;16;31;15;19]", 
                        "[0;20;8;9;10;11;12;13;14;22;1;2;3;4;5;6;7;23;16;24;15;25;18;26;17;19]", 
                        "[0;21;17;27;18;28;16;29;15;30;8;9;10;11;12;13;14;31;1;2;3;4;5;6;7;19]", 
                        "[0;20;15;22;16;23;18;24;17;25;8;9;10;11;12;13;14;26;1;2;3;4;5;6;7;19]", 
                        "[0;21;18;27;17;28;16;29;15;30;1;2;3;4;5;6;7;31;8;9;10;11;12;13;14;19]", 
                        "[0;20;1;2;3;4;5;6;7;22;8;9;10;11;12;13;14;23;18;24;17;25;16;26;15;19]", 
                        "[0;21;15;27;16;28;1;2;3;4;5;6;7;29;8;9;10;11;12;13;14;30;18;31;17;19]", 
                        "[0;21;1;2;3;4;5;6;7;27;8;9;10;11;12;13;14;28;15;29;16;30;18;31;17;19]", 
                        "[0;20;16;22;15;23;17;24;18;25;1;2;3;4;5;6;7;26;8;9;10;11;12;13;14;19]", 
                        "[0;20;18;22;17;23;15;24;16;25;8;9;10;11;12;13;14;26;1;2;3;4;5;6;7;19]", 
                        "[0;21;15;27;16;28;8;9;10;11;12;13;14;29;1;2;3;4;5;6;7;30;18;31;17;19]", 
                        "[0;21;15;27;16;28;1;2;3;4;5;6;7;29;8;9;10;11;12;13;14;30;17;31;18;19]", 
                        "[0;21;18;27;17;28;15;29;16;30;1;2;3;4;5;6;7;31;8;9;10;11;12;13;14;19]", 
                        "[0;20;16;22;15;23;17;24;18;25;8;9;10;11;12;13;14;26;1;2;3;4;5;6;7;19]", 
                        "[0;21;8;9;10;11;12;13;14;27;1;2;3;4;5;6;7;28;17;29;18;30;16;31;15;19]", 
                        "[0;20;16;22;15;23;18;24;17;25;1;2;3;4;5;6;7;26;8;9;10;11;12;13;14;19]", 
                        "[0;21;8;9;10;11;12;13;14;27;1;2;3;4;5;6;7;28;18;29;17;30;16;31;15;19]", 
                        "[0;20;15;22;16;23;1;2;3;4;5;6;7;24;8;9;10;11;12;13;14;25;18;26;17;19]", 
                        "[0;21;16;27;15;28;18;29;17;30;8;9;10;11;12;13;14;31;1;2;3;4;5;6;7;19]", 
                        "[0;20;15;22;16;23;17;24;18;25;1;2;3;4;5;6;7;26;8;9;10;11;12;13;14;19]"
))

我想做的是向 data.frame 添加一个额外的列,它告诉我,如果第一个条件、第二个条件、以下两个条件都满足或都不满足:

  1. 17 的行在序列中比 15 晚。
  2. 18 的行在序列中比 16 晚。

中,Ronak 给出了以下非常好的解决方案:

library(dplyr)

data %>%
  mutate(result = case_when(grepl('15.*17', col) & grepl('16.*18', col) ~ 'Both', 
                            grepl('15.*17', col) ~ 'First', 
                            grepl('16.*18', col) ~ 'Second', 
                            TRUE ~ 'Neither'))

后来我意识到,我想稍微改变一下条件;

  1. 17 的行在序列中比 15 晚, 之间最多有三个其他数字。
  2. 18 的行在序列中比 16 晚, 之间最多有一个其他数字。

我一直在查看 regular expressions,但我真的不知道如何调整模式。这主要是因为我不知道中间有多少个双位数和个位数。

例如,我想做类似 /^[.]{0,10}$/ 的事情,但我不知道中间会有多少个字符。有人能帮忙吗?

这是你需要的吗?

data %>%
  mutate(result = case_when(grepl('15(;\d+;){,3}17', seq) & grepl('16(;\d+;){,1}18', seq) ~ 'Both', 
                            grepl('15(;\d+;){,3}17', seq) ~ 'First', 
                            grepl('16(;\d+;){,1}18', seq) ~ 'Second', 
                            TRUE ~ 'Neither'))
    seq  result
1  [0;20;1;2;3;4;5;6;7;22;8;9;10;11;12;13;14;23;17;24;18;25;15;26;16;19] Neither
2  [0;21;16;27;15;28;8;9;10;11;12;13;14;29;1;2;3;4;5;6;7;30;18;31;17;19] Neither
3  [0;20;15;22;16;23;18;24;17;25;1;2;3;4;5;6;7;26;8;9;10;11;12;13;14;19]  Second
4  [0;20;8;9;10;11;12;13;14;22;1;2;3;4;5;6;7;23;15;24;16;25;17;26;18;19] Neither
5  [0;21;18;27;17;28;15;29;16;30;8;9;10;11;12;13;14;31;1;2;3;4;5;6;7;19] Neither
6  [0;20;1;2;3;4;5;6;7;22;8;9;10;11;12;13;14;23;15;24;16;25;17;26;18;19] Neither
7  [0;21;1;2;3;4;5;6;7;27;8;9;10;11;12;13;14;28;17;29;18;30;16;31;15;19] Neither
8  [0;20;8;9;10;11;12;13;14;22;1;2;3;4;5;6;7;23;16;24;15;25;18;26;17;19] Neither
9  [0;21;17;27;18;28;16;29;15;30;8;9;10;11;12;13;14;31;1;2;3;4;5;6;7;19] Neither
10 [0;20;15;22;16;23;18;24;17;25;8;9;10;11;12;13;14;26;1;2;3;4;5;6;7;19]  Second
11 [0;21;18;27;17;28;16;29;15;30;1;2;3;4;5;6;7;31;8;9;10;11;12;13;14;19] Neither
12 [0;20;1;2;3;4;5;6;7;22;8;9;10;11;12;13;14;23;18;24;17;25;16;26;15;19] Neither
13 [0;21;15;27;16;28;1;2;3;4;5;6;7;29;8;9;10;11;12;13;14;30;18;31;17;19] Neither
14 [0;21;1;2;3;4;5;6;7;27;8;9;10;11;12;13;14;28;15;29;16;30;18;31;17;19]  Second
15 [0;20;16;22;15;23;17;24;18;25;1;2;3;4;5;6;7;26;8;9;10;11;12;13;14;19]   First
16 [0;20;18;22;17;23;15;24;16;25;8;9;10;11;12;13;14;26;1;2;3;4;5;6;7;19] Neither
17 [0;21;15;27;16;28;8;9;10;11;12;13;14;29;1;2;3;4;5;6;7;30;18;31;17;19] Neither
18 [0;21;15;27;16;28;1;2;3;4;5;6;7;29;8;9;10;11;12;13;14;30;17;31;18;19] Neither
19 [0;21;18;27;17;28;15;29;16;30;1;2;3;4;5;6;7;31;8;9;10;11;12;13;14;19] Neither
20 [0;20;16;22;15;23;17;24;18;25;8;9;10;11;12;13;14;26;1;2;3;4;5;6;7;19]   First
21 [0;21;8;9;10;11;12;13;14;27;1;2;3;4;5;6;7;28;17;29;18;30;16;31;15;19] Neither
22 [0;20;16;22;15;23;18;24;17;25;1;2;3;4;5;6;7;26;8;9;10;11;12;13;14;19] Neither
23 [0;21;8;9;10;11;12;13;14;27;1;2;3;4;5;6;7;28;18;29;17;30;16;31;15;19] Neither
24 [0;20;15;22;16;23;1;2;3;4;5;6;7;24;8;9;10;11;12;13;14;25;18;26;17;19] Neither
25 [0;21;16;27;15;28;18;29;17;30;8;9;10;11;12;13;14;31;1;2;3;4;5;6;7;19] Neither
26 [0;20;15;22;16;23;17;24;18;25;1;2;3;4;5;6;7;26;8;9;10;11;12;13;14;19] Neither