R dplyr-按多个条件过滤 [英] R dplyr - filter by multiple conditions

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

问题描述

我有如下所示的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,年龄> 80),但是我不知道如何同时在两个条件下使用它? p>

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?

推荐答案

下面的方法也可以使用 dplyr

Following may help you here too using dplyr

library(dplyr)
##Creating variable dat here which has values in it.
dat <- read.table(text = "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",
                  header = TRUE)

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

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

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