简体   繁体   English

如何强制 R 中的 Hmisc package 舍入到小数点后 3 位?

[英]How to force Hmisc package in R to round to 3 decimals?

I have a correlation matrix produced using the Hmisc package.我有一个使用Hmisc package 生成的相关矩阵。 It produces the correlations to 2 decimal places, however I would like it to show 3 decimals places.它产生 2 个小数位的相关性,但是我希望它显示 3 个小数位。 How do I force it to do so?我如何强迫它这样做? I am using the rcorr function.我正在使用rcorr function。

Data:数据:

df <- structure(list(X1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 
    X2 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0), Y1 = c(3.93333333333333, 3.13333333333333, 4.3, 4.13333333333333, 
    3.2, 3.6, 3.66666666666667, 1.8, 3.8, 4.13333333333333, 4.13333333333333, 
    1.6, 3.4, 3.26666666666667, 2.53333333333333, 4.06666666666667, 
    4.53333333333333, 4.13333333333333, 3.4, 3.8, 3.33333333333333, 
    3.86666666666667, 4, 4.2, 2.53333333333333, 1.73333333333333, 
    1.8, 2.73333333333333, 1.66666666666667, 1.33333333333333, 
    2, 2.4, 3, 3.26666666666667, 3.2, 3.53333333333333, 3.66666666666667, 
    2.8, 3.33333333333333, 3.06666666666667, 3.46666666666667, 
    3.13333333333333, 3.93333333333333, 2.46666666666667, 1.26666666666667, 
    4.13333333333333, 1.8, 3, 2.93333333333333, 1.53333333333333, 
    4.06666666666667, 3.6, 2.06666666666667, 4.13333333333333, 
    3.3, 3.53333333333333, 3.4, 3.93333333333333, 3.73333333333333, 
    3, 3.13333333333333, 2.2, 4, 5, 3.66666666666667, 3.2, 3.4, 
    3.8, 3.66666666666667, 4.3, 4.2, 4.46666666666667, 3.33333333333333, 
    4.4, 4.2), Y2 = c(3.6, 2.2, 3.5, 4.2, 3, 2.8, 5, 2, 4.8, 
    4.4, 4.6, 1.6, 3.8, 3, 3, 3.4, 3.8, 4.2, 3.4, 3.4, 3.4, 4, 
    4.8, 4, 2.8, 1, 1.4, 1.2, 1.6, 3.8, 2.2, 1.4, 3.2, 1, 3.4, 
    3.2, 3.4, 1.8, 3.2, 1, 3, 2.8, 2.4, 1, 1, 4, 1.8, 2, 1, 1.2, 
    4.4, 3.2, 2, 4.2, 3.2, 3.2, 3.2, 3.6, 2.2, 2.8, 3.4, 2.6, 
    3.8, 4.2, 2.8, 3, 3.2, 4.8, 4.8, 4, 5, 5, 4.2, 4.6, 4.5)), row.names = c(NA, 
-75L), groups = structure(list(filter = c(0, 1, 2), .rows = list(
    1:25, 26:50, 51:75)), row.names = c(NA, -3L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

correlation.matrix <- as.matrix(df[, c(
  "X1", "X2", "Y1", "Y2")])
cor.table <- Hmisc::rcorr(correlation.matrix)
cor.table

I think that you cannot (natively).我认为你不能(本机)。

From thesource code of Hmisc:::print.rcorr (not exported but used as an S3 method):Hmisc:::print.rcorr的源代码(未导出但用作 S3 方法):

  P <- ifelse(P < .0001, 0, P)
  p <- format(round(P, 4))

The function hard-codes replacing low values with 0 as well as 4 digits in the print. function 硬编码用 0 和 4 位数字替换低值。 I suggest you open an issue with a feature-request.我建议您使用功能请求打开一个问题

In the meantime, here's a replacement that allows you to control it a little:同时,这里有一个替代品,可以让您稍微控制它:

print.rcorr <- function(x, ..., digits = getOption("Hmisc.rcorr.digits", 4))
{
  print(round(x$r,2))
  n <- x$n
  if(all(n == n[1,1]))
    cat("\nn=", n[1,1], "\n\n")
  else {
    cat("\nn\n")
    print(n)
  }

  cat("\nP\n")
  P <- x$P
  P <- ifelse(P < .0001, 0, P)
  p <- format(round(P, digits))
  p[is.na(P)] <- ""
  print(p, quote=FALSE)
  invisible()
}

print(cor.table, digits=2)
#       X1    X2    Y1    Y2
# X1  1.00 -0.50  0.31  0.34
# X2 -0.50  1.00 -0.50 -0.61
# Y1  0.31 -0.50  1.00  0.74
# Y2  0.34 -0.61  0.74  1.00
# n= 75 
# P
#    X1   X2   Y1   Y2  
# X1      0.00 0.01 0.00
# X2 0.00      0.00 0.00
# Y1 0.01 0.00      0.00
# Y2 0.00 0.00 0.00     

This "should" always mask Hmisc:::print.rcorr .这个“应该”总是掩盖Hmisc:::print.rcorr I added the ability to use an options controller, so you can even do我添加了使用options controller 的功能,所以你甚至可以这样做

options("Hmisc.rcorr.digits" = 2)
cor.table
#       X1    X2    Y1    Y2
# X1  1.00 -0.50  0.31  0.34
# X2 -0.50  1.00 -0.50 -0.61
# Y1  0.31 -0.50  1.00  0.74
# Y2  0.34 -0.61  0.74  1.00
# n= 75 
# P
#    X1   X2   Y1   Y2  
# X1      0.00 0.01 0.00
# X2 0.00      0.00 0.00
# Y1 0.01 0.00      0.00
# Y2 0.00 0.00 0.00     

However, two words of caution:但是,请注意两点:

  • though I didn't test well and I think it's robust enough, it might be possible for some method of loading packages to use Hmisc:::print.rcorr instead of your global-defined version... I'll have to think a little about namespaces and search path to know if this can unintentionally happen;虽然我没有很好地测试并且我认为它足够强大,但某些加载包的方法可能会使用Hmisc:::print.rcorr而不是你的全局定义版本......我不得不考虑一下关于名称空间和搜索路径很少知道这是否会意外发生; and
  • if/when Hmisc updates their function, you should probably update this function as well.如果/当Hmisc更新其 function 时,您可能也应该更新此 function。 While this is unlikely (the last update to that function was 6 years ago ), it is certainly feasible.虽然这不太可能(function 的最后一次更新是6 年前),但它肯定是可行的。

Using the print function, you can specify the number of digits you need.使用print function,您可以指定所需的位数。

print( cor.table$r, digits=3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM