user11538509
user11538509

Reputation:

Usings multiple conditions in select (dplyr)

I want to choose certain columns of a dataframe with dplyr::select() using contains() more than ones. I know there are other ways to solve it, but I wonder whether this is possible inside select(). An example:

df <- data.frame(column1= 1:10, col2= 1:10, c3= 1:10)
library(dplyr)
names(select(df, contains("col") & contains("1")))

This gives an error but I would like the function to give "column1".

I expected that select() would allow a similiar appraoch as filter() where we can set multiple conditions with operators, i.e. something like filter(df, column1 %in% 1:5 & col2 != 2).

EDIT

I notice that my question is more general and I wonder whether it is possible to pass any combinations in select(), like select(df, contains("1") | !starts_with("c")), and so on. But can't figure out how to make such a function.

Upvotes: 1

Views: 5002

Answers (3)

Matt
Matt

Reputation: 2987

You can use select_if and grepl

library(dplyr)

df %>% 
select_if(grepl("col", names(.)) & grepl(1, names(.)))


#   column1
#1        1
#2        2
#3        3
#4        4
#5        5
#6        6
#7        7
#8        8
#9        9
#10      10

If you want to use select with contains you could do something like this:

df %>% 
select(intersect(contains("col"), contains("1")))

This can be combined in other ways, as mentioned in the comments:

df %>% 
select(intersect(contains("1"), starts_with("c")))

Upvotes: 6

M.Viking
M.Viking

Reputation: 5398

You could use the dplyr::intersect function

select(df, intersect(contains("col"), contains("1")))

Upvotes: 0

Gabriel M. Silva
Gabriel M. Silva

Reputation: 750

You can also chain two select calls:

library(dplyr)

df <- data.frame(column1 = 1:10, col2 = 1:10, c3 = 1:10)

df %>% 
  select(contains("col")) %>% 
  select(contains("1"))

Not too elegant for one-line lovers

Upvotes: 1

Related Questions