chippycentra
chippycentra

Reputation: 879

Drop columns by patterns values in R

I have a dataframe such as :

g1   g2   g3   g4   g5
2    0    1    0    1
2    1    1    0    1
2    1    1    2    1

and I would like to remove each column that have at least one 2 in its values.

and get a new df :

g2   g3   g5
0    1    1
1    1    1
1    1    1

Thanks for your help.

Upvotes: 4

Views: 80

Answers (3)

NelsonGon
NelsonGon

Reputation: 13309

We can also use purrr:

df[,map_lgl(df,~sum(.x==2)<1)]


   g2 g3 g5
1  0  1  1
2  1  1  1
3  1  1  1

Upvotes: 1

www
www

Reputation: 39154

We can use the select_if function with a predicate function to achieve this.

library(dplyr)

dat2 <- dat %>% select_if(function(x) !any(x == 2))
dat2
#   g2 g3 g5
# 1  0  1  1
# 2  1  1  1
# 3  1  1  1

DATA

dat <- read.table(text = "g1   g2   g3   g4   g5
2    0    1    0    1
                  2    1    1    0    1
                  2    1    1    2    1",
                      header = TRUE, stringsAsFactors = FALSE)

Upvotes: 3

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521028

We can use colSums here:

df <- data.frame(g1=c(2,2,2), g2=c(0,1,1), g3=c(1,1,1), g4=c(0,0,2), g5=c(1,1,1))
df[, !colSums(df==2)]

  g2 g3 g5
1  0  1  1
2  1  1  1
3  1  1  1

The idea is to include all rows from the original data frame, but only include those columns which never have a value of 2. In that case, the call to colSums() would be zero, which is logically equivalent to false in R.

Upvotes: 5

Related Questions