根据应用于多个列的单个条件对数据框进行子集 [英] Subset a dataframe based on a single condition applied to multiple columns

查看:60
本文介绍了根据应用于多个列的单个条件对数据框进行子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了该站点上现有的子集Q& A,却找不到我想要的东西。

I've had a look through the existing subset Q&A's on this site and couldn't quite find what I was looking for.

我想基于一种条件(例如,如果值小于5)对数据帧进行子集化。但是,我只希望所有列中的值都低于5的行。

I want to subset a data frame based on one condition (e.g. if the value is below 5). However, I only want the rows where the value in all of the columns is below 5.

例如,使用 iris 数据集-我想选择第1-3列的值都低于5的所有行。

For example using the iris dataset - I would like to select all the rows where columns 1-3 all have values below 5.

subdata <- iris[which(iris[,1:3]<5),]

不要为我做。在子集数据的底部有很多NA行。

This doesn't do it for me. I get lots of NA rows at the bottom of the subset data.

任何帮助都非常感谢!

推荐答案

尝试

subdata <- iris[apply(iris[,1:3] < 5, 1, all),]

这篇关于根据应用于多个列的单个条件对数据框进行子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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