我的矢量看起来像这些变化:
cn1 <- c("Probe","Genes","foo","bar","Probe","Genes","foo","bar")
# 0 1 2 3 4 5 6 7
cn2 <- c("Probe","Genes","foo","bar","qux","Probe","Genes","foo","bar","qux")
# 0 1 2 3 4 5 6 7 8 9
请注意,上面的每个向量由两部分组成。它们以"Probe","Genes"
分隔。
我想要做的是获取该分隔符之间的条目的第一部分的索引。产生
cn1_id ------> [2,3]
cn2_id ------> [2,3,4]
我如何在R中实现这一目标?
我试过了,但它并没有做我想要的事情:
> split(cn1,c("Probe","Genes"))
$Genes
[1] "Genes" "bar" "Genes" "bar"
$Probe
[1] "Probe" "foo" "Probe" "foo"
答案 0 :(得分:2)
这是您可以使用的功能。请注意,R向量是基于1的,因此计数从1开始而不是0.
findidx <- function(x) {
idx <- which(x=="Probe" & c(tail(x,-1),NA)=="Genes")
if (length(idx)>1) {
(idx[1]+2):(idx[2]-1)
} else {
NA # what to return if no match found
}
}
findidx(cn1)
# [1] 3 4
findidx(cn2)
# [1] 3 4 5
答案 1 :(得分:1)
您可以尝试between
data.table
indx <- between(cn1, 'Genes', 'Probe')
indx2 <- between(cn2, 'Genes', 'Probe')
which(cumsum(indx)==2)[-1]-1
#[1] 2 3
which(cumsum(indx2)==2)[-1]-1
#[1] 2 3 4