Limiting the number of decimals in a dataframe (R)

放肆的年华 提交于 2020-12-28 14:56:27

问题


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:

Value 

0.202021561664556
0.202021561664556
0.202021561664556
0.202021561664556
...

My expected dataframe

Value
0.20202156
0.20202156
0.20202156
0.20202156
...

Real input (DF) that not works:

DF <- "NE001358.Log.R.Ratio
    -0.0970369274475688
    0.131893549586039
    0.0629266495860389
    0.299559132381831
    -0.0128804337656807
    0.0639743960526874
    0.0271669351886552
    0.322395363972391
    0.179591292893632"

DF <- read.table(text=DF, header = TRUE)

回答1:


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)))



回答2:


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)




回答3:


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)




回答4:


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



来源:https://stackoverflow.com/questions/23217520/limiting-the-number-of-decimals-in-a-dataframe-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!