我有以下数据:
> dput(s1[1:10,])
structure(list(V1 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), V2 = c(0,
0.55, 0.9, 3.125, 5, 19.96666667, 12.25, 35.15, 5.4, 58.58)),
na.action = structure(260:270, .Names = c("260",
"261", "262", "263", "264", "265", "266", "267", "268", "269",
"270"), class = "omit"), row.names = c("33", "317", "6",
"202","250", "185", "28", "251", "218", "116"), class =
"data.frame")
我想计算 V2(第二列)在至少 3 个连续时间步长内低于 1 时连续出现的次数。
所以在上面的例子中,答案应该是1。
如果我对上述数据重新采样:
b<-a[sample(1:nrow(a),replace=T),]
dput(b)
structure(list(V1 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), V2 = c(5,
35.15, 0.55, 19.96666667, 0.55, 19.96666667, 0.55, 3.125, 0.9,
0.55)), na.action = structure(260:270, .Names = c("260",
"261","262", "263", "264", "265", "266", "267", "268", "269",
"270"), class = "omit"), row.names = c("250", "251", "317",
"185","317.1", "185.1", "317.2", "202", "6", "317.3"), class =
"data.frame")
这里,答案应该是0。
如何使用 R 中的一行命令获取这些值?这可能吗?
我将不胜感激任何帮助。
最佳答案
使用rle
:
with(rle(s1$V2 < 1), sum(lengths[values] >= 3))
#[1] 1
关于r - 如何在R中计算低于阈值的连续出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61400988/