Batch convert columns to numeric type

前端 未结 6 2018
眼角桃花
眼角桃花 2021-01-01 11:20

I have a dataframe with a bunch of columns that I need to convert to the numeric type. I have written the following code to try to do this, however it is saying the replacem

6条回答
  •  伪装坚强ぢ
    2021-01-01 11:42

    You can use sapply for this:

    dat <- sapply( dat, as.numeric )
    

    If not every column needs converting:

    library( taRifx )
    dat <- japply( dat, which(sapply(dat, class)=="character"), as.numeric )
    

提交回复
热议问题