Search code examples
rdataframeconditional-statements

separating factor and character columns in r data frame using multiple condition


I would like to separate only two types of a column of the data frame (factors and characters) and the following codes results in an error. I wonder what is the correct way to carry out this task.

install.packages("dslabs")
library(dslabs)
data(murders)

names(murders)[sapply(murders, (is.factor | is.character))]

I can use the following code to combine the two, but I wonder what will be the correct format in case I want to use multiple conditions as the code above.

c(names(murders)[sapply(murders, is.factor)],names(murders)[sapply(murders, is.character)])

Solution

  • We may need a lambda function

    names(murders)[sapply(murders, function(x) 
          is.factor(x) | is.character(x))]
    

    Or another option is to get the class and use %in%

    names(murders)[sapply(murders, class) %in% c("factor", "character")]
    

    Or with Filter

    names(Filter(function(x) is.factor(x)|is.character(x), murders))
    

    Or using tidyverse

    library(dplyr)
    murders %>%
        select(where(~ is.factor(.x)|is.character(.x))) %>%
        names