如何识别R中的序列

这个问题是
R – identify consecutive sequences的延伸

我有一个数据框,我需要只保留那些在ROI列中我有连续序列_aCORRECT1和_CORRECT1的试验.无论_aCORRECT1和_CORRECT1出现多少次都可以重复.

在下面的例子中,我可以保留ntrial 78和201,因为_aCORRECT1后跟_CORRECT1.
但是,我需要删除ntrial 10和400.在试验10中_aCORRECT1后面没有_CORRECT1.在试验400中_CORRECT1之前没有_aCORRECT1.

非常感谢!

subject ROI                 ntrial 
sbj05   ff                  78     
sbj05   as                  78     
sbj05   fgfsd               78     
sbj05   sgf                 78     
sbj05   jh                  78     
sbj05   sgsgsfg             78     
sbj05   fgsfg               78     
sbj05   sgf_aCORRECT1       78     
sbj05   dfs_CORRECT1        78     
sbj05   ffg                 78     
sbj05   sdfdsf              78     
sbj05   sl                  78     
sbj05   wgrt                78     
sbj05   qswefrd             201    
sbj05   ssdg                201    
sbj05   sdgfdsg             201    
sbj05   sgsgd               201    
sbj05   sgsdg               201    
sbj05   dd_aCORRECT1        201    
sbj05   dd_aCORRECT1        201    
sbj05   ffds_CORRECT1       201    
sbj05   ffds_CORRECT1       201    
sbj05   ffds_CORRECT1       201    
sbj05   hy                  201    
sbj05   gfg                 201    
sbj05   nbc                 201    
sbj05   cvbvn               10     
sbj05   kpj                 10     
sbj05   nbvnb               10     
sbj05   mnm                 10     
sbj05   dghsfh_aCORRECT1    10     
sbj05   gdh                 10   
sbj05   fgjj                10     
sbj05   gnjdg               10     
sbj05   gf                  10     
sbj05   qw                  400    
sbj05   vfs                 400    
sbj05   zx                  400    
sbj05   zvzv                400    
sbj05   zvzv_CORRECT1       400    
sbj05   zvzd_CORRECT1       400    
sbj05   zvv                 400    
sbj05   cv                  400    
sbj05   v                   400    
sbj05   mngy                400    

最佳答案 使用dplyr,df1是一个数据帧,告诉你应该保留哪些ntrial值.这是通过为aCORRECT和_CORRECT设置逻辑指示符并检查每个分组的ntrial是否存在相邻值来完成的.然后df2是仅包含有效ntrial的df的版本

df1 <- df %>% mutate(aCOR=grepl("aCORRECT",ROI),COR=grepl("_CORRECT",ROI)) %>%
              group_by(ntrial) %>% summarise(keep=any(aCOR & lead(COR)))

df2 <- df[df$ntrial %in% df1$ntrial[df1$keep],]


df1
# A tibble: 4 × 2
  ntrial  keep
   <int> <lgl>
1     10 FALSE
2     78  TRUE
3    201  TRUE
4    400 FALSE

df2
   subject           ROI ntrial
1    sbj05            ff     78
2    sbj05            as     78
3    sbj05         fgfsd     78
4    sbj05           sgf     78
5    sbj05            jh     78
6    sbj05       sgsgsfg     78
7    sbj05         fgsfg     78
8    sbj05 sgf_aCORRECT1     78
9    sbj05  dfs_CORRECT1     78
10   sbj05           ffg     78
11   sbj05        sdfdsf     78
12   sbj05            sl     78
13   sbj05          wgrt     78
14   sbj05       qswefrd    201
15   sbj05          ssdg    201
16   sbj05       sdgfdsg    201
17   sbj05         sgsgd    201
...
点赞