【发布时间】:2010-12-02 09:54:10
【问题描述】:
我有一个逻辑向量,我希望在特定索引处插入新元素。我在下面想出了一个笨拙的解决方案,但是有没有更简洁的方法?
probes <- rep(TRUE, 15)
ind <- c(5, 10)
probes.2 <- logical(length(probes)+length(ind))
probes.ind <- ind + 1:length(ind)
probes.original <- (1:length(probes.2))[-probes.ind]
probes.2[probes.ind] <- FALSE
probes.2[probes.original] <- probes
print(probes)
给予
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
和
print(probes.2)
给予
[1] TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE FALSE
[13] TRUE TRUE TRUE TRUE TRUE
所以它可以工作,但看起来很丑 - 有什么建议吗?
【问题讨论】:
-
为什么需要插入?
-
长话短说,但基本上是在寻找 TRUE 的运行,但有预定的地方我想中断运行。我第一次使用“rle”,但它的扩展性很差,所以想出了这个脏矢量解决方案
标签: r