返回连续值满足条件的行 [英] Return rows where consecutive values meet criterion

查看:72
本文介绍了返回连续值满足条件的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据框 df 。我想返回一个向量 result ,该向量指示哪些行满足以下条件:该行中至少有2个连续值小于-1.7。

I have the following dataframe df. I would like to return a vector result that indicates which rows meet the following criterion: at least 2 consecutive values in that row are lower than -1.7.

set.seed(123)

df <- data.frame(V1=rnorm(10,-1.5,.5),
                 V2=rnorm(10,-1.5,.5),
                 V3=rnorm(10,-1.5,.5),
                 V4=rnorm(10,-1.5,.5),
                 V5=rnorm(10,-1.5,.5),
                 V6=rnorm(10,-1.5,.5),
                 V7=rnorm(10,-1.5,.5),
                 V8=rnorm(10,-1.5,.5),
                 V9=rnorm(10,-1.5,.5),
                 V10=rnorm(10,-1.5,.5))
rownames(df) <- c(seq(1976,1985,1))

结果将是一个向量:

result <- c(1977,1979,1980,1982,1983,1985)


推荐答案

一种选择是循环通过使用 apply 的行,使用 rle ,检查是否有任何个具有个长度的TRUE元素大于1,提取名称

One option is to loop through the rows with apply, create a logical condition with rle, check if there are any TRUE elements that have lengths more than 1, extract the names

names(which(apply(df, 1, function(x) with(rle(x < - 1.7), any(lengths[values] > 1)))))
#[1] "1977" "1979" "1980" "1982" "1983" "1985"






更好的方法是通过放置两个逻辑矩阵(即删除数据集的第一列,检查它是否小于-1.7,类似地删除最后一列并执行相同的操作), Reduce 使其成为单个逻辑<$ c $通过检查相应元素是否为 TRUE c> matrix ,获取 rowSums ,如果值大于0,我们提取行名称


Or a better approach is to vectorize it by placing two logical matrices (i.e. remove the first column of the dataset, check whether it is less than -1.7, similarly remove the last column and do the same), Reduce it to a single logical matrix by checking whether the corresponding elements are TRUE, get the rowSums, if the value is greater than 0, we extract the row names

names(which(rowSums(Reduce(`&`, list(df[-ncol(df)] < -1.7, df[-1] < -1.7))) > 0))
#[1] "1977" "1979" "1980" "1982" "1983" "1985"

这篇关于返回连续值满足条件的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆