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

时间:2019-03-02 20:01:17

标签: r truncate read.csv magrittr kableextra

人们有很多线程认为他们在读取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

1 个答案:

答案 0 :(得分: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}