Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Drop columns that match a pattern, with an exception

I have example data as follows:

dat <- mtcars
dat$add <- dat[,1]
dat$last <- dat[,1]
names(dat) <- c("fraction_low", "fraction_medium", "fraction_high", "fraction_low_2000", "fraction_medium_2000", "fraction_high_2000","fraction_low_2001", "fraction_medium_2001", "fraction_high_2001","fraction_low_comb", "fraction_medium_comb", "fraction_high_comb", "last")

I would like to remove all columns/variable names that have fraction and low, medium, or high in their name, except the columns/variable names that also have comb in the name.

I found a lot of good answers for removing columns with a certain pattern here. For example:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

library(dplyr)
dat %>% select( -( contains("fraction") & ( (contains("low") | contains("medium") | contains("high") ) )))

But how should I implement and exception to such a pattern?

Desired output:

desired_output <- dat[,10:13]

                    fraction_low_comb fraction_medium_comb fraction_high_comb last
Mazda RX4                           4                    4               21.0 21.0
Mazda RX4 Wag                       4                    4               21.0 21.0
Datsun 710                          4                    1               22.8 22.8
Hornet 4 Drive                      3                    1               21.4 21.4

>Solution :

We may use

library(dplyr)
 dat %>%
   select(-matches(c("fraction", "low", "high", "medium")), contains("comb")) 

Or may use

dat %>% 
   select(-matches("fraction_(low|medium|high)_(?!comb)|fraction_(low|medium|high)$",
     perl = TRUE)) 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading