If I want to convert an integer
or double
variables to character
variables,
how can I accomplish the task, I tried the below code, but I a
You can use this version of mutate_if
library(dplyr)
storms %>% mutate_if(~ is.double(.) | is.integer(.), as.character)
which would convert the double or integer columns to character.
We can do this with base R
storms[] <- lapply(storms, function(x) if(is.numeric(x)) as.character(x) else x)
Or using data.table
library(data.table)
setDT(storms)[, names(storms) := lapply(.SD, function(x)
if(is.numeric(x)) as.character(x) else x)]