R中多列中的过滤值 [英] Filter value in Multiple Columns in R

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

问题描述

我试图同时基于多个列中的向量值(来自循环)来过滤数据帧。

I am trying to filter a data frame based on a vector value (that comes from a loop) in multiple columns at the same time.

循环中,是以下相关步骤:

As this takes place in a loop, here is the pertinent steps:

name.id = NamesList$`First Name` [i]

在上面,我确定了要在此循环中传递的唯一行(名称)。

In the above, I identify the unique row (name) I want to pass in this loop.

接下来,我想针对我的数据框(在此示例中称为test.df)过滤该值,以查找所有以'x'开头的列(因为实际应用中超过3个)目标名称在 A组中出现的行。

Next, I want to filter that value against my dataframe (referenced as test.df in this example) to find in all the columns that start with an 'x' (as there will be more than 3 in the real application) the rows where the targeted name appears in group 'A'.

output.df = test.df %>% filter(grepl('A', Group) & (c(x1, x2, x3) %in% name.id))

例如,使用示例数据下面,对于循环的第一遍, JOE将被标识并过滤。我知道我可以创建一个长列表,上面写着x1%in%name.id,x2%in%name.id,但是会有5列以上,而且我知道有一种比这更优雅的方法来引用要过滤的列。

For example, using the sample data below, for the first pass of the loop, 'JOE' will be the name identified and filtered. I know I could create a long list saying x1 %in% name.id, x2 %in% name.id, but there will be 5+ columns and I know there's a more elegant way to reference the columns to filter than this.

样本数据:

x1 <- c('JOE','JOE','JOE','JOE', 'JOE', 'JOE', 'JOE', 'JOE', 'JOE', 'JOE', '','','','', 'FRED','FRED','FRED','FRED', 'FRED','FRED','JOE','JOE', 'FRED','FRED','JOE','JOE')

x2 <- c('ERIC','ERIC','ERIC','ERIC', 'ERIC', 'ERIC', 'ERIC', 'ERIC', 'ERIC', 'ERIC', '','','','', 'JOE','JOE','JOE','RON', 'RON','RON','RON','RON', 'RON','RON','FRED','FRED')

x3 <- c('SARAH','SARAH','SARAH','SARAH', 'SARAH', 'SARAH', 'SARAH', 'JOE', 'JOE', 'JOE', 'JOE','','','', 'JAY','JAY','JAY','JAY', 'JAY','JAY','JAY','JAY','JAY','','RON','RON')

State <- c('1','1','1','1', '1', '1', '1', '1', '1', '1', '2','2','2','2', '2','2','2','2', '2','2','2','2', '2','1','1','1')

Group <- c('A','A','A','B', 'B', 'B', 'A', 'B', 'A', 'B', 'A','A','A','B', 'A','A','A','B', 'NA','B','B','B', 'B', 'A','B','A')

test.df=cbind.data.frame(x1, x2, x3, State, Group)


推荐答案

使用 tidyverse

require(tidyverse)

dta <- data_frame(State, Group, x1, x2, x3)
dta %>% 
    gather(key = "key", value = "value", x1:x3) %>%
    filter(value %in% [condition to match])

gather 函数移动三列 x1 x2 x3 分成包含键值对的两列。然后,您可以仅在值列上进行过滤。

The gather function moves the three columns x1, x2, x3 into two columns comprising key-value pairs. You can then filter on the value column alone.

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

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