如何在向量中的两个字符串之间grep所有字符串

时间:2018-03-22 17:49:57

标签: r

我有一个角色矢量:

FINDINGS<-c("FD_BarrettDetail", "The segment looks flat.", "Prague score C0M3", 
"It is x cm in length.", "This is an ultra-short segment.", "FD_BarrettDetail1_Benign", 
"The segment looks flat.", "No nodularity is present.", "It is a long segment.", 
"Some areas of vascular abnormalities are seen.", "The pit pattern is normal.")

我想提取FD_BarrettDetailFD_BarrettDetail1_Benign之间的所有值(不包括分隔符),以便我得到:

Extract<-c("The segment looks flat.", "Prague score C0M3", 
"It is x cm in length.", "This is an ultra-short segment.")

1 个答案:

答案 0 :(得分:3)

使用==which创建开始和结束的索引,以捕获此间隔内的所有内容。

> start <- which(FINDINGS=="FD_BarrettDetail")+1
> end <- which(FINDINGS=="FD_BarrettDetail1_Benign")-1
> FINDINGS[start:end]
[1] "The segment looks flat." "Prague score C0M3" "It is x cm in length."          
[4] "This is an ultra-short segment."
>