I am trying to get the class type of the columns of a data frame. What I am doing is:
sapply(mydata,class)
But now, I want to find only those column names which are factor. I tried the following:
sapply(data,is.factor)
But it gives me:
ResponseFlag Gender Marital OccupInput
False True True False
How can I separate column names which are factor?
Any help or idea will be appreciated.
Try this:
Filter(is.factor, mydata)
names only If you just want the names:
names(Filter(is.factor, mydata))
or
names(iris)[ sapply(iris, is.factor) ]
dplyr These can alternately be expressed using dplyr like this:
library(dplyr)
mydata %>% Filter(f = is.factor)
mydata %>% Filter(f = is.factor) %>% names
mydata %>% summarise_each(funs(is.factor)) %>% unlist %>% .[.] %>% names
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With