Limiting the number of decimals in a dataframe (R)

后端 未结 4 1837
青春惊慌失措
青春惊慌失措 2021-02-05 08:26

I would like to limit the number of decimals when a data frame is imported. My .txt input have 16 decimals to each row in collumn \"Value\". My dataframe look like that:

相关标签:
4条回答
  • 2021-02-05 09:04

    I have a dataframe of 13 columns where the 1st 2 columns are integers and the rest of the columns are numeric with decimals. I want the decimal values alone to be restricted to 2 decimal places. Applying @G. Grothendieck 's method above, a simple solution below:

    DF[, 3:13] <- round(DF[, 3:13], digits = 2)

    0 讨论(0)
  • 2021-02-05 09:05

    A dplyr solution using mutate_if to check if the columns in the current data frame are numeric then apply the round() function to them

    # install.packages('dplyr', dependencies = TRUE)
    library(dplyr)
    
    DF %>% 
      mutate_if(is.numeric, round, digits = 8)
    #>   NE001358.Log.R.Ratio
    #> 1          -0.09703693
    #> 2           0.13189355
    #> 3           0.06292665
    #> 4           0.29955913
    #> 5          -0.01288043
    #> 6           0.06397440
    #> 7           0.02716694
    #> 8           0.32239536
    #> 9           0.17959129
    
    ### dplyr v1.0.0+
    DF %>% 
      mutate(across(where(is.numeric), ~ round(., digits = 8)))
    #>   NE001358.Log.R.Ratio
    #> 1            -0.097037
    #> 2             0.131894
    #> 3             0.062927
    #> 4             0.299559
    #> 5            -0.012880
    #> 6             0.063974
    #> 7             0.027167
    #> 8             0.322395
    #> 9             0.179591
    

    Created on 2019-03-17 by the reprex package (v0.2.1.9000)

    0 讨论(0)
  • 2021-02-05 09:14

    just throw a copy of this into the path of your project in a utils directory and source it when you run your script

    "formatColumns" <-
     function(data, digits)
     {
        "%,%" <- function(x,y)paste(x,y,sep="")
        nms <- names(data)
        nc <- ncol(data)
        nd <- length(digits)
        if(nc!=nd) 
          stop("Argument 'digits' must be vector of length " %,% 
               nc %,% ", the number of columns in 'data'.")
        out <- as.data.frame(sapply(1:nc, 
                             FUN=function(x, d, Y)
                             format(Y[,x], digits=d[x]), Y=tbl, d=digits))
        if(!is.null(nms)) names(out) <- nms
        out
    }
    

    Now you can sit back and relax

    formatColumns(MyData, digits=c(0,2,4,4,4,0,0))
    

    et cetera et cetera et cetera

    0 讨论(0)
  • 2021-02-05 09:20

    Here is.num is TRUE for numeric columns and FALSE otherwise. We then apply round to the numeric columns:

    is.num <- sapply(DF, is.numeric)
    DF[is.num] <- lapply(DF[is.num], round, 8)
    

    If what you meant was not that you need to change the data frame but just that you want to display the data frame to 8 digits then it's just:

    print(DF, digits = 8)
    

    In dplyr 1.0.0 and later one can use across within mutate like this:

    library(dplyr)
    DF %>% mutate(across(is.numeric, ~ round(., 8)))
    
    0 讨论(0)
提交回复
热议问题