6

I am trying to select only factor columns from my data frame. Example is below:

bank[,apply(bank[,names(bank)!="id"], is.factor)]

But the code behaves strangely. Step by step:

sapply(bank[,names(bank)!="id"], is.factor)

I get:

age         sex      region      income     married    children         car 
      FALSE        TRUE        TRUE       FALSE        TRUE       FALSE        TRUE 
   save_act current_act    mortgage         pep      ageBin 
       TRUE        TRUE        TRUE        TRUE        TRUE 

Looks OK. Now, I assume that I just pass this matrix of TRUE/FALSE to the next step and get only the columns I need:

bank[,sapply(bank[,names(bank)!="id"], is.factor)]

But as result I get all the same columns as in original bank dataframe. Nothing is filtered out. I tried it one way or another but can't find a solution. Any advice on what I am doing wrong?

Maksim Khaitovich
  • 4,742
  • 7
  • 39
  • 70
  • @d.b just a dataframe with a bunch of columns – Maksim Khaitovich Mar 31 '17 at 19:18
  • @d.b nope, I load it from csv. Nothing special about it. – Maksim Khaitovich Mar 31 '17 at 19:20
  • 3
    You should provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). That will make it easier to help you. The TUE/FALSE values should match up to all your columns. By excluding the "id" column, you are shifting everything. – MrFlick Mar 31 '17 at 19:20

2 Answers2

7
#DATA
df = mtcars
colnames(df) = gsub("mpg","id",colnames(df))
df$am = as.factor(df$am)
df$gear = as.factor(df$gear)
df$id = as.factor(df$id)

#Filter out 'id' after selecting factors
df[,sapply(df, is.factor) & colnames(df) != "id"]
d.b
  • 32,245
  • 6
  • 36
  • 77
4
df = mtcars
colnames(df) = gsub("mpg","id",colnames(df))
df$am = as.factor(df$am)
df$gear = as.factor(df$gear)
df$id = as.factor(df$id)

library(dplyr)
df %>%  select_if(is.factor) %>% select(-id)
MLEN
  • 2,162
  • 2
  • 20
  • 36