按多个条件过滤 [英] Filter by multiple conditions

查看:59
本文介绍了按多个条件过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下所示的data.frame

I've got data.frame like below

ID  country age
1   X   83
2   X   15
3   Y   2
4   Y   12
5   X   2
6   Y   2
7   Y   18
8   X   85

我需要过滤年龄在10岁以下且同时在80岁以上的行.我如何以最简单的方式做到这一点?对于一种情况,我可以使用 filter(data.frame,age> 80),但是我不知道如何同时针对两种情况进行操作?

I need to filter rows for age below 10 and at the same time above 80. How can I do it in the simplest way? For one condition I can use filter(data.frame, age > 80) but I don't know how to do it for two conditions at the same time?

推荐答案

从问题中我不确定您是否要使用介于10和80之间的值,还是希望小于10并大于80的值.在 filter 中放置多个参数.如果您想要10以下和80以上的值,可以将 | 用作或"运算符:

I'm not sure from the question if you want the values between 10 and 80 or those below ten and above 80. If you want those between, you can put multiple arguments in filter. If you want those below 10 and above 80 you can use | as an "or" operator:

library(tidyverse)

data %>%
  filter(age > 10,
         age < 80)

data %>%
  filter(age < 10 | age > 80)

这篇关于按多个条件过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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