简体   繁体   English

R - 从 dataframe 中排除不包含某些值的行

[英]R - Exclude rows from dataframe that don't contain certain values

I have a huge dataframe from which I need to remove rows that don't contain any values present in a vector (vector name "codes").我有一个巨大的 dataframe ,我需要从中删除不包含向量中存在的任何值的行(向量名称“代码”)。

Example dataframe:示例 dataframe:

df <- data.frame(ID = as.integer(c(10001, 10002, 10004, 10005, 10006)), 
                 X1 = as.integer(c(150, 120, 175, 160, 1)),
                 X2 = as.integer(c(1, 1412415, 16420, 19920, 150)))
> df
     ID  X1      X2
1 10001 150       1
2 10002 120 1412415
3 10003 175   16420
4 10004 160   19920
5 10005   1     150

codes <- c(120, 150)
codes <- as.integer(codes)

I have tried multiple options, here's one failed example:我尝试了多种选择,这是一个失败的例子:

newdf <- df[do.call(paste, df[2:3]) %in% codes,]

> newdf
[1] ID X1 X2
<0 rows> (or 0-length row.names)

Instead, newdf should contain rows 1, 2 and 5 with ID numbers 10001, 10002 and 10005 as such:相反,newdf 应该包含 ID 号为 10001、10002 和 10005 的第 1、2 和 5 行,如下所示:

> newdf
     ID  X1      X2
1 10001 150       1
2 10002 120 1412415
5 10005   1     150

This way is nice, as it is scaleable to any number of columns that begin with X.这种方式很好,因为它可以扩展到以 X 开头的任意数量的列。

dplyr::filter_at(df, vars(starts_with("X")), any_vars(. %in% codes))

dplyr::filter() does what you want. dplyr::filter()做你想做的事。 It takes in any condition (like is the value part of vector X) and removes all rows not matching the conditions:它接受任何条件(例如向量 X 的值部分)并删除所有不匹配条件的行:

library(dplyr)

df %>%
filter(X1 %in% codes | X2 %in% codes)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有条件地聚合不包含某些值的数据框行 - Conditionally aggregate dataframe rows that don't contain certain values 从数据框中删除不包含任何数值的行 - Remove rows from dataframe that don't contain any numeric values r data.table-排除行中包含某些值的组 - r data.table - exclude groups that contain certain values in rows 如果行包含某个值 (R,dplyr),则从 dataframe 中删除行 - Remove rows from dataframe if they contain a certain value (R ,dplyr) 只更新数据集1中某些行的值,使用R中数据集2中的值更新-但仅匹配不匹配的那些,否则保留原始行吗? - update values of only certain rows in dataset1, with values from dataset2 in R - but only the ones that don't match, or else keep the original? 在 R 如何包含 dataframe 中包含某些关键字的行 - In R how do I include rows from dataframe that contain certain keywords 如何删除包含R中某些单词的数据框中的行? - How to remove rows in a dataframe that contain certain words in R? 返回所有不包含特定值的行 - Returning all rows that don't contain a certain value 删除包含高于R中某个切割点的值的行 - Remove rows that contain values above a certain cut point in R 在 R 的多列中过滤仅包含某些值的行 - filtering rows that only contain certain values among multiple columns in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM