R想要限制csv文件的位数

问题描述 投票:0回答:1

有很多关于人们认为在读取csv文件时丢失数字的线索,并且它只是一个数字设置,并没有显示所有这些。

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

这导致我试图在突出显示之前进行回合,但是如果因为我使用接近0的数字而低于4个位置,这会导致不良结果。它还会不时地抛出科学数字......在我的桌子上看起来不干净。

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
r truncate read.csv magrittr kableextra
1个回答
2
投票

如果我理解你想要什么,这应该工作:

首先,让我们使用formatC将您的数字列转换为具有所需小数位数的字符向量

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

请注意,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}
© www.soinside.com 2019 - 2024. All rights reserved.