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 ofcondition
), 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!
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)
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