在R中,计算序列中相同元素运行的最有效/最简单的方法是什么?
例如,如何计算非负整数序列中连续零的数量:
x <- c(1,0,0,0,1,0,0,0,0,0,2,0,0) # should give 3,5,2
Run Code Online (Sandbox Code Playgroud) 我有一个 data.table 如下:
library(data.table)
dt.tst <- CJ(Type = c("A", "B"),
Range_val = seq(0,20000, by = 1000))
dt.tst[Range_val == 2000 & Type == "A", Value := 0.987]
dt.tst[Range_val == 2000 & Type == "B", Value := 1.987]
dt.tst[Range_val == 9000 & Type == "A", Value := 1.056]
dt.tst[Range_val == 9000 & Type == "B", Value := 2.138]
dt.tst[Range_val == 16000 & Type == "A", Value := 1.563]
dt.tst[Range_val == 16000 & Type == "B", Value := 2.089]
Run Code Online (Sandbox Code Playgroud)
我想在值列中填写 NA::