R - 识别连续序列 [英] R - identify consecutive sequences
问题描述
我有一个包含 107635 行和 3 列的大文件:主题、感兴趣区域 (ROI) 和试验次数.ROI 可以是 A、B、C、D、E、F.我想要做的是只保留那些在 ROI 列中我有 B、C、D 连续序列的试验,当 B 第一次出现时.B、C 和 D 出现多少次并不重要.
I have a big file with 107635 rows, and 3 columns: subject, regions of interest (ROIs), and number of the trial. The ROIs can be A, B, C, D, E, F. What I want to do is to keep only those trials where in the column ROI I have a consecutive sequence of B, C, D, the first time that B appears. It doesn't matter how many times B, C and D occur.
在下面的例子中,我可以保留 ntrial 78 和 201,因为第一次出现 B 之后是 C 和 D.但是,我需要删除ntrial 10 和400.在trial 10 中,B、C 和D 不连续.审判400中B第一次出现,B后面没有C和D.
In the example below, I can keep ntrial 78 and 201, because the first time that B appeared was followed by C and D. However, I need to remove the ntrial 10 and 400. In the trial 10 B, C and D are not consecutive. In the trial 400 the first time that B appears, B is not followed by C and D.
对于输出,我只需要一个值为 1 的列来保留每一行中的试验,并为与要删除的试验对应的行设置一个值为 0.
For the output, I just need a column with a value of 1 for the trials to keep, in each row, and a value of 0 for the rows corresponding to the trials to remove.
关于如何创建可以自动化程序的代码,而无需目视检查每次试验的任何建议?
Any suggestion on how to create a code that can automatise the procedure, without visually inspect each trial?
非常感谢!
subject ROI ntrial output
sbj05 A 78 1
sbj05 A 78 1
sbj05 A 78 1
sbj05 A 78 1
sbj05 A 78 1
sbj05 A 78 1
sbj05 B 78 1
sbj05 B 78 1
sbj05 C 78 1
sbj05 D 78 1
sbj05 E 78 1
sbj05 E 78 1
sbj05 E 78 1
sbj05 A 201 1
sbj05 A 201 1
sbj05 A 201 1
sbj05 A 201 1
sbj05 A 201 1
sbj05 B 201 1
sbj05 C 201 1
sbj05 D 201 1
sbj05 E 201 1
sbj05 E 201 1
sbj05 E 201 1
sbj05 F 201 1
sbj05 F 201 1
sbj05 A 10 0
sbj05 A 10 0
sbj05 A 10 0
sbj05 A 10 0
sbj05 B 10 0
sbj05 A 10 0
sbj05 C 10 0
sbj05 D 10 0
sbj05 E 10 0
sbj05 E 10 0
sbj05 A 400 0
sbj05 A 400 0
sbj05 A 400 0
sbj05 B 400 0
sbj05 A 400 0
sbj05 B 400 0
sbj05 C 400 0
sbj05 C 400 0
sbj05 C 400 0
sbj05 D 400 0
sbj05 E 400 0
sbj05 E 400 0
sbj05 D 400 0
推荐答案
这是使用 data.table
和 stringi
首先,我定义了一些辅助函数,它可以帮助我检测每组 B
的第一次准确率,并验证它们后面的序列是否正确
First, I'm defining some helper function that will help me detect first accurances of B
per group and validate that they are followed by the correct sequence
Myfunc <- function(x) {
which(x == "B")[1L] ==
stri_locate_first_regex(paste(x, collapse = ""), 'B*CD')[, 1L]
}
然后,实现是直截了当的
Then, the implementation is straight forward
library(data.table)
library(stringi)
setDT(df)[, if(Myfunc(ROI)) .SD, by = .(subject, ntrial)]
# subject ntrial ROI
# 1: sbj05 78 A
# 2: sbj05 78 A
# 3: sbj05 78 A
# 4: sbj05 78 A
# 5: sbj05 78 A
# 6: sbj05 78 A
# 7: sbj05 78 B
# 8: sbj05 78 B
# 9: sbj05 78 C
# 10: sbj05 78 D
# 11: sbj05 78 E
# 12: sbj05 78 E
# 13: sbj05 78 E
# 14: sbj05 201 A
# 15: sbj05 201 A
# 16: sbj05 201 A
# 17: sbj05 201 A
# 18: sbj05 201 A
# 19: sbj05 201 B
# 20: sbj05 201 C
# 21: sbj05 201 D
# 22: sbj05 201 E
# 23: sbj05 201 E
# 24: sbj05 201 E
# 25: sbj05 201 F
# 26: sbj05 201 F
<小时>
或者,如果你只想要一个额外的列,你可以做
Or, if you just want an additional column you could do
setDT(df)[, output := +Myfunc(ROI), by = .(subject, ntrial)]
这篇关于R - 识别连续序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!