首发于“生信补给站” ,https://mp.weixin.qq.com/s/l9Ci7wREQWpEV5dTvKuoHg,更多的R统计,绘图,生信,请移步🤭
上篇根据 msleep
数据集,介绍了列的操作,盘一盘Tidyverse| 筛行选列之select,玩转列操作
本文盘一盘行的筛选 🤭
一 载入R包,数据
#载入R包
#install.packages("tidyverse")
library("tidyverse")
#查看内置数据集
head(msleep,2)
dim(msleep)
#[1] 83 11
本示例数据集很小,实际中数据量很大,可以根据使用filter()函数筛选出后续需要的“行”子集。
二 基于数值变量筛选
基于数值,根据 >
, >=
, <
, <=
, ==
和 !=
完成筛选
1 筛选sleep_total 大于 18
msleep %>% select(name, sleep_total) %>%
filter(sleep_total > 18) %>% head(2)
# A tibble: 2 x 2
name sleep_total
<chr> <dbl>
1 Big brown bat 19.7
2 Thick-tailed opposum 19.4
2 筛选sleep_total在16和18之间
可以filter(sleep_total >= 16, sleep_total <= 18)
,也可以使用between
函数
msleep %>% select(name, sleep_total) %>%
filter(between(sleep_total, 16, 18)) %>% head(2)
# A tibble: 2 x 2
name sleep_total
<chr> <dbl>
1 Owl monkey 17
2 Long-nosed armadillo 17.4
3 筛选 均值±3sd 范围内**
near()
选择”中心值“,tol()