R fill vector efficiently

后端 未结 4 707
不思量自难忘°
不思量自难忘° 2021-01-22 20:22

I have a fairly big vector (>500,000 in length). It contains a bunch of NA interspersed with 1 and it is always guaranteed that it begins with 1<

4条回答
  •  抹茶落季
    2021-01-22 20:42

    It may not be faster, but v1[i] <- v1[i-1] * (cmp[i-1] == 0) avoids all explicit "if" calls. I can't test it right now, but you might try @James solution vs. looping over this form for, say a vector of 1e4 length to see which executes faster.

提交回复
热议问题