mutate_if() with OR conditions dplyr

后端 未结 2 1525
孤街浪徒
孤街浪徒 2021-01-23 16:22

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

2条回答
  •  不知归路
    2021-01-23 17:17

    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)]
    

提交回复
热议问题