How to convert entire dataframe to numeric while preserving decimals?

前端 未结 4 635
甜味超标
甜味超标 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 04:54

    You might need to do some checking. You cannot safely convert factors directly to numeric. as.character must be applied first. Otherwise, the factors will be converted to their numeric storage values. I would check each column with is.factor then coerce to numeric as necessary.

    df1[] <- lapply(df1, function(x) {
        if(is.factor(x)) as.numeric(as.character(x)) else x
    })
    sapply(df1, class)
    #         a         b 
    # "numeric" "numeric" 
    
    0 讨论(0)
  • 2020-12-13 04:59
    df2 <- data.frame(apply(df1, 2, function(x) as.numeric(as.character(x))))
    
    0 讨论(0)
  • 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" 
    
    0 讨论(0)
  • 2020-12-13 05:19

    Using dplyr (a bit like sapply..)

    df2 <- mutate_all(df1, function(x) as.numeric(as.character(x)))
    

    which gives:

    glimpse(df2)
    Observations: 4
    Variables: 2
    $ a <dbl> 0.01, 0.02, 0.03, 0.04
    $ b <dbl> 2, 4, 5, 7
    

    from your df1 which was:

    glimpse(df1)
    Observations: 4
    Variables: 2
    $ a <fctr> 0.01, 0.02, 0.03, 0.04
    $ b <dbl> 2, 4, 5, 7
    
    0 讨论(0)
提交回复
热议问题