gpt4 book ai didi

r - 如何计算R中低于阈值的连续出现

转载 作者:行者123 更新时间:2023-12-04 10:09:23 25 4
gpt4 key购买 nike

我有以下数据:

> 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com