如何在R中过滤一系列数字? [英] How do I filter a range of numbers in R?

查看:80
本文介绍了如何在R中过滤一系列数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有如下所示的数据框Mydata:

Let's say I have the data frame Mydata as shown below:

Mydata <- data.frame(x = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
                     y = c(20, 30, 45, 54, 65, 78, 97, 102, 123, 156))

我要过滤此数据帧并创建另一个数据帧,以便仅显示37之间的x值及其对应的y值.我尝试了以下操作:

I want to filter this data frame and create another data frame, so that only the values of x between 3 and 7 and their corresponding y values are shown. I attempted the following:

new_frame <- Mydata %>% filter(x == (3:7))

这不起作用.然后如何过滤指定范围?

This didn't work. How then would I filter for a specified range?

提前感谢所有帮助

推荐答案

使用%in%

library(dplyr)
new_frame<- Mydata%>% filter(x %in% (3:7))
new_frame
#   x  y
# 1 3 45
# 2 4 54
# 3 5 65
# 4 6 78
# 5 7 97

这篇关于如何在R中过滤一系列数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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