r - 基于R中的多个字符串列过滤行
问题描述
给定如下数据框:
city type count
0 bj a 10
1 bj a 23
2 bj b 12
3 bj c 34
4 sh a 17
5 sh b 18
6 sh c 25
7 sh c 13
8 sh a 12
我想根据city
和type
:过滤行bj-a, bj-c, sh-b
,预期的结果将是这样的:
city type count
0 bj a 10
1 bj a 23
2 bj c 34
3 sh b 18
我怎么能在 R 中做到这一点?谢谢。
解决方案
您可以使用subset
:
subset(df, city == 'bj' & type %in% c('a', 'c') | city == 'sh' & type == 'b')
# city type count
#0 bj a 10
#1 bj a 23
#3 bj c 34
#5 sh b 18
或filter
在dplyr
:
library(dplyr)
df %>%
filter(city == 'bj' & type %in% c('a', 'c') |
city == 'sh' & type == 'b')
推荐阅读
- java - 无法设置背景颜色
- python-3.x - 我正在尝试从多个 python 文件和其他文件(图像、excel 电子表格)生成一个独立的 exe 文件
- excel - Excel 新手,#REF!错误对我没有意义。
- r - 比较同一列上的行
- javascript - 如何在javascript中摆脱数组中的数组
- r - 使用 lapply 时功能的工作方式不同
- android - Room + 找不到实现 DB + DB_Impl 不存在
- node.js - Angular Nodejs MySql CORS 问题
- java - 播放服务器音乐时冻结android应用
- c# - 大型数据集上的 Kendo Grid Export to Excel 错误