R 想要限制 csv 文件中的位数

R wanting to limit the amount of digits from csv file

有很多关于人们认为他们在读取 csv 文件时丢失了数字的话题,而这只是一个数字设置,并没有显示所有数字。

另一方面,我想将输入的数据四舍五入或截断到小数点后两位。我在 Rmarkdown 中遇到问题,我无法在突出显示字段后限制小数位。

这导致我试图在突出显示之前四舍五入,但如果我低于 4 个位置,这会导致不良结果,因为我使用的是接近 0 的数字。它还会不时地输入科学数字时间....在我的 table.

中看起来不太干净

df.csv

     Sample   blank   square   stool   ball       triangle     circle    hammer     dog
 1:    16-ww3 0.00090 0.93100 0.01219 0.00006      0.00606      0.00180 0.00000 0.00003
 2:      17-e 0.00034 0.67452 0.00297 0.00006      0.00357      0.00172 0.00008 0.00001
 3:    21-r9a 0.00186 0.34577 0.01558 0.00020      0.02277      0.00586 0.00009 0.00012
 4:       7-d 0.00003 0.00352 0.01179 0.00003      0.01640      0.56326 0.00349 0.00064
 5:    7401-1 0.00151 0.55153 0.00196 0.00017      0.00055      0.00029 0.00012 0.00000
 6:    7401-2 0.00056 0.50825 0.00433 0.00010      0.00000      0.00008 0.00006 0.00003

代码

library(kableExtra)
library(magrittr)

DF <- read.csv(file="df.csv"), header=TRUE, sep=",",stringsAsFactors = FALSE)

DF[1:nrow(DF), 2:ncol(DF)] <- round(DF[1:nrow(DF), 2:ncol(DF)], 4)

paint <- function(x) {
  ifelse(x < 0.1, "white", ifelse(x < 0.2, "yellow", "red"))
}

DF %<>%
  mutate_if(is.numeric, function(x) {
   cell_spec(x, background = paint(x), format = "latex") 
  })

kable(DF, caption = "Highlighted numbers near zero", digits = 2, format = "latex", booktabs = T, escape = F, longtable = T)%>%
  kable_styling(latex_options = c("striped", "hold_position", "repeat_header", font_size = 6))%>%
  landscape()%>%
  row_spec(0, angle = 45)

期望的输出

 numbers formatted like:  0.00  0.01  0.10  1.10  
 AND highlighted as described in the paint function

如果我理解正确你想要什么,这应该有效:

首先让我们使用 formatC

将您的数字列转换为具有所需小数位数的字符向量
DF[, -1] = lapply(DF[, -1], formatC, format = 'f', flag='0', digits = 2)

现在我们可以使用以下方法应用 Latex 格式:

DF[,-1] = lapply(DF[,-1], function(x) cell_spec(x, background = paint(x), format = "latex"))

请注意,paint 仍然适用于字符向量,因为与数值的比较会将它们强制转换为数值。

这是一个完整的可重现示例,在实践中证明了这一点

set.seed(1234)
DF <- data.frame(V1 = sample(letters,10,T), V2 = abs(rnorm(10)), V3 = abs(rnorm(10)))
DF[, -1] = lapply(DF[, -1], formatC, format = 'f', flag='0', digits = 2)
DF[, -1] = lapply(DF[,-1], function(x) cell_spec(x, background = paint(x), format = "latex"))
#    V1                       V2                        V3
# 1   c   \cellcolor{red}{0.51} \cellcolor{yellow}{0.11}
# 2   q   \cellcolor{red}{0.57}    \cellcolor{red}{0.51}
# 3   p   \cellcolor{red}{0.55}    \cellcolor{red}{0.91}
# 4   q   \cellcolor{red}{0.56}    \cellcolor{red}{0.84}
# 5   w   \cellcolor{red}{0.89}    \cellcolor{red}{2.42}
# 6   q   \cellcolor{red}{0.48} \cellcolor{yellow}{0.13}
# 7   a   \cellcolor{red}{1.00}    \cellcolor{red}{0.49}
# 8   g   \cellcolor{red}{0.78}    \cellcolor{red}{0.44}
# 9   r \cellcolor{white}{0.06}    \cellcolor{red}{0.46}
# 10  n   \cellcolor{red}{0.96}    \cellcolor{red}{0.69}

kable(DF, caption = "Highlighted numbers near zero", 
  digits = 2, format = "latex", booktabs = T, escape = F, longtable = T) %>%
  kable_styling(latex_options = c("striped", "hold_position", 
    "repeat_header", font_size = 6)) %>%
  landscape() %>%
  row_spec(0, angle = 45)

# \begin{landscape}
# \begin{longtable}{lll}
# \caption{\label{tab:}Highlighted numbers near zero}\
# \toprule
# \rotatebox{45}{V1} & \rotatebox{45}{V2} & \rotatebox{45}{V3}\
# \midrule
# \endfirsthead
# \caption[]{Highlighted numbers near zero \textit{(continued)}}\
# \toprule
# \rotatebox{45}{V1} & \rotatebox{45}{V2} & \rotatebox{45}{V3}\
# \midrule
# \endhead
# \
# \endfoot
# \bottomrule
# \endlastfoot
# \rowcolor{gray!6}  c & \cellcolor{red}{0.51} & \cellcolor{yellow}{0.11}\
# q & \cellcolor{red}{0.57} & \cellcolor{red}{0.51}\
# \rowcolor{gray!6}  p & \cellcolor{red}{0.55} & \cellcolor{red}{0.91}\
# q & \cellcolor{red}{0.56} & \cellcolor{red}{0.84}\
# \rowcolor{gray!6}  w & \cellcolor{red}{0.89} & \cellcolor{red}{2.42}\
# \addlinespace
# q & \cellcolor{red}{0.48} & \cellcolor{yellow}{0.13}\
# \rowcolor{gray!6}  a & \cellcolor{red}{1.00} & \cellcolor{red}{0.49}\
# g & \cellcolor{red}{0.78} & \cellcolor{red}{0.44}\
# \rowcolor{gray!6}  r & \cellcolor{white}{0.06} & \cellcolor{red}{0.46}\
# n & \cellcolor{red}{0.96} & \cellcolor{red}{0.69}\*
#   \end{longtable}
# \end{landscape}