r - 有条件地保留和删除 R 中的行
问题描述
我正在使用 R。假设我有以下数据:
day_of_the_year = seq(as.Date("2010/1/1"), as.Date("2010/1/31"),by="day")
col_1 = rnorm(31,50,5)
col_2 = rnorm(31,100,5)
my_file = data.frame(day_of_the_year, col_1, col_2)
如果满足一组条件,我正在尝试学习如何保留行和删除行,例如:
library(dplyr)
omit <- c("2010-01-12", "2010-01-16", "2010-01-20", "2010-01-25")
#keep rows if condition is met
final <- filter(my_file, !(day_of_the_year %in% omit | col_1 > 50 | col_2 >100))
#delete rows if condition is met
final <- filter(my_file, (day_of_the_year %in% omit | col_1 > 50 | col_2 >100))
问题:这是在 R 中执行此操作的正确方法吗?还有哪些其他方法可以做到这一点?
谢谢
解决方案
推荐阅读
- cmake - CPack WIX 在安装树之外安装一个文件
- ios - 用户说话时自动开始录音
- postgresql - 如何在 Kubernetes 中为 PostgreSQL 数据使用 PersistentVolume
- c# - 如何在忙碌时显示指示器
- javascript - 如何在我的应用程序中循环图像
- c# - Linux下运行时ASP.NET Core异常(已添加相同键)
- react-native - React-Native 导航选项给出错误
- jenkins - 由于 nuget 无法连接到远程服务器,构建失败
- javascript - 如何使用 javascript 将上午或下午的 24 小时时间格式转换为 12 小时
- sql - 在 MS Access 上使用 SQL 将行数据转换为列