rdataframe

convert all factor columns to character in a data.frame without affecting non-factor columns


For example I have a data.frame with both integer column and factor columns.

data <- data.frame(
    a = 1:5, 
    b = factor(c("a", "b", "c", "d", "e")), 
    c = factor(c("f", "g", "h", "i", "j")))`

And I want to convert b and c factor columns into character, with keeping column a as integer.

I tried the follwing code :

sapply(data, function(x) {if_else(is.factor(x), as.character(x), x)})

Instead of getting the result, it throws an error:

Error: true must be length 1 (length of condition), not 5

I knew this can be done by mutate_if function in dplyr. But I still wonder why my code does not work for this case.

Thank you very much for any suggestions!


Solution

  • You can try:

    library(tidyverse)
    d <- data.frame(a = 1:5, 
                    b = factor(c("a", "b", "c", "d", "e")), 
                    c = factor(c("f", "g", "h", "i", "j")))
    d %>% glimpse() 
    Observations: 5
    Variables: 3
    $ a <int> 1, 2, 3, 4, 5
    $ b <fctr> a, b, c, d, e
    $ c <fctr> f, g, h, i, j
    
    d %>% 
      mutate_if(is.factor, as.character) %>% 
      glimpse()
    Observations: 5
    Variables: 3
    $ a <int> 1, 2, 3, 4, 5
    $ b <chr> "a", "b", "c", "d", "e"
    $ c <chr> "f", "g", "h", "i", "j"
    

    Using base R you can try

    d[] <- lapply(d, function(x) if(is.factor(x)) as.character(x) else x)