Batch convert columns to numeric type

前端 未结 6 2160
眼角桃花
眼角桃花 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:57

    Combining the answer by @Andrii with the comment by @RobertYi, my short suggestion is

    df[] <- sapply(df, as.numeric)
    

    This ensures that the result stays as data frame.

提交回复
热议问题