How to convert entire dataframe to numeric while preserving decimals?

前端 未结 4 634
甜味超标
甜味超标 2020-12-13 04:49

I have a mixed class dataframe (numeric and factor) where I am trying to convert the entire data frame to numeric. The following illustrates the type of data I am working w

4条回答
  •  既然无缘
    2020-12-13 05:16

    > df2 <- data.frame(sapply(df1, function(x) as.numeric(as.character(x))))
    > df2
         a b
    1 0.01 2
    2 0.02 4
    3 0.03 5
    4 0.04 7
    > sapply(df2, class)
            a         b 
    "numeric" "numeric" 
    

提交回复
热议问题