简体   繁体   English

dplyr过滤器基于列名称查找特定值

[英]dplyr filter for a specific values based on column names

How can I filter a dataframe based on certain columns. 如何根据某些列过滤数据框。 So I want to find the columns with 'Test' in their colname Then filter them so that I only retain those that have a certain value. 因此,我想在它们的同名中找到带有“ Test”的列,然后对其进行过滤,以便仅保留那些具有特定值的列。

# Temp Data
df <- as.data.frame(matrix(seq(1:40),ncol=10,nrow=40))
colnames(df) <- c("V1", "V2", "V3 - Test", "V4 - Test", "V5", "V6", "V7", "V8", "V9 - Test", "V10")

# What I thought would work
library(dplyr)

df %>%
  filter(grepl("Test", colnames(df) ) == 40 ) %>%
  select(-contains("Test"))

Note the real dataset has about 40 columns and 30k rows that. 注意实际的数据集大约有40列和3万行。

Reshaping will also work. 重塑也将起作用。

library(dplyr)
library(tidyr)

df_ID = df %>% mutate(ID = 1:n())

df_ID %>%
  select(contains("Test"), ID) %>%
  gather(variable, value, -ID) %>%
  filter(value == 40) %>%
  semi_join(df_ID)

We can try 我们可以试试

df[!rowSums(df[grepl("Test", names(df))]!=40),]

Or using dplyr 或使用dplyr

library(dplyr)
library(magrittr)
df %>%
   mutate(ind =!rowSums(.[grep('Test', names(.))]!=40)) %>%
   .$ind %>% 
   extract(df, .,)
#    V1 V2 V3 - Test V4 - Test V5 V6 V7 V8 V9 - Test V10
# 40 40 40        40        40 40 40 40 40        40  40

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM