3 回答

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊
這里有一個(gè)方法,建立在約書亞的rle方法:(編輯以使用seq_len和lapply按馬立克的建議)
> (!x) * unlist(lapply(rle(x)$lengths, seq_len))
[1] 0 1 0 1 2 3 0 0 1 2
更新。只是為了踢,這是另一種方法,大約快5倍:
cumul_zeros <- function(x) {
x <- !x
rl <- rle(x)
len <- rl$lengths
v <- rl$values
cumLen <- cumsum(len)
z <- x
# replace the 0 at the end of each zero-block in z by the
# negative of the length of the preceding 1-block....
iDrops <- c(0, diff(v)) < 0
z[ cumLen[ iDrops ] ] <- -len[ c(iDrops[-1],FALSE) ]
# ... to ensure that the cumsum below does the right thing.
# We zap the cumsum with x so only the cumsums for the 1-blocks survive:
x*cumsum(z)
}
試試一個(gè)例子:
> cumul_zeros(c(1,1,1,0,0,0,0,0,1,1,1,0,0,1,1))
[1] 0 0 0 1 2 3 4 5 0 0 0 1 2 0 0
現(xiàn)在比較百萬長(zhǎng)度向量的時(shí)間:
> x <- sample(0:1, 1000000,T)
> system.time( z <- cumul_zeros(x))
user system elapsed
0.15 0.00 0.14
> system.time( z <- (!x) * unlist( lapply( rle(x)$lengths, seq_len)))
user system elapsed
0.75 0.00 0.75
故事的道德:?jiǎn)涡懈?,更容易理解,但并不總是最快?/p>

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超9個(gè)贊
William Dunlap關(guān)于R-help的帖子是尋找與跑步長(zhǎng)度相關(guān)的所有事情的地方。他在這篇文章中的f7 是
f7 <- function(x){ tmp<-cumsum(x);tmp-cummax((!x)*tmp)}
在目前的情況下f7(!x)
。在性能方面有
> x <- sample(0:1, 1000000, TRUE)> system.time(res7 <- f7(!x)) user system elapsed 0.076 0.000 0.077 > system.time(res0 <- cumul_zeros(x)) user system elapsed 0.345 0.003 0.349 > identical(res7, res0)[1] TRUE

TA貢獻(xiàn)1850條經(jīng)驗(yàn) 獲得超11個(gè)贊
rle 將“計(jì)算自上一次非零以來該值連續(xù)多少小時(shí)”,但不是“所需輸出”的格式。
請(qǐng)注意相應(yīng)值為零的元素的長(zhǎng)度:
rle(x)
# Run Length Encoding
# lengths: int [1:6] 1 1 1 3 2 2
# values : num [1:6] 1 0 1 0 1 0
- 3 回答
- 0 關(guān)注
- 551 瀏覽
添加回答
舉報(bào)