r - 我如何找到连续的值

标签 r dataframe vector

假设我有这个向量:

x <- c(1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0)

我想找出我们连续 ¨1¨ 3 次的次数。 为此,我正在使用此代码-

n.hot <- which(df, df$vector = 1,1,1)

我该怎么做才能让它真正发挥作用? 谢谢

最佳答案

如果 1 1 1 1 对你来说意味着 1 1 1 实际上出现了两次,你可以这样做:

sum(stats::filter(x, c(1, 1, 1), circular = TRUE, sides = 2) == 3)

[1] 7

关于r - 我如何找到连续的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60077737/

相关文章:

r - 检查 R 数据框中每个变量的数据类型

r - 如何在R中将随机效应模型与主题随机拟合?

r - R 中的高级运行时间计算

r - 如何解决R中的 "NAs are not allowed in subscripted assignments"问题

dataframe - 如何根据 Julia 中的其他列值有条件地在数据框中创建新列

python - 无法对列数据重新排序

c++ - Dlib:如何初始化一个忽略标志设置为 1 的 mmod_rect?

c++ - 通过引用传递截断的 vector

python - 收到错误 cx_Oracle.DatabaseError : ORA-01008: not all variables bound while trying to bind the value of a list in a merge statement

c++ - 我如何根据另一个预先排序的字符串 vector 来排序包含字符串的 vector ?