Home > Software design >  How to filter DataFrame columns by value keeping original column order?
How to filter DataFrame columns by value keeping original column order?

Time:01-10

I'm trying to filter a DataFrame, keeping only columns containing "_time" or "___" in their column names.

I tried using df %>% select(contains(c("_time", "___")). However, this changes the order of the columns in the output, where all columns with _time are displayed first and the columns with "___" are displayed last.

How can filtering be done without changing the column order?

CodePudding user response:

We can use matches

library(dplyr)
df %>%
   select(matches("_time|___"))

-output

  h_time l_time f___d m_time s___hello
1     11     16    21     26        31
2     12     17    22     27        32
3     13     18    23     28        33
4     14     19    24     29        34
5     15     20    25     30        35

compared to

df %>%
  select(contains(c("_time", "___")))
  h_time l_time m_time f___d s___hello
1     11     16     26    21        31
2     12     17     27    22        32
3     13     18     28    23        33
4     14     19     29    24        34
5     15     20     30    25        35

data

df <- data.frame(col1 = 1:5, col2 = 6:10, h_time = 11:15, 
l_time = 16:20, f___d = 21:25, m_time = 26:30, 
col_new = 41:45, s___hello = 31:35)

CodePudding user response:

Base R: Data from @akrun (many thanks)

df[,grepl("_time|___", colnames(df))]
  h_time l_time f___d m_time s___hello
1     11     16    21     26        31
2     12     17    22     27        32
3     13     18    23     28        33
4     14     19    24     29        34
5     15     20    25     30        35
  •  Tags:  
  • r
  • Related