R 想要限制来自 csv 文件的位数 [英] R wanting to limit the amount of digits from csv file

查看:23
本文介绍了R 想要限制来自 csv 文件的位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

There are plenty of threads about people thinking they have lost digits when reading in a csv file, and it's just a digits setting that isn't displaying all of them.

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

I on the other hand want to round or truncate my incoming data to two decimal places. I am having an issue in Rmarkdown where I can't limit the decimal places after highlighting fields.

这导致我试图在突出显示之前进行四舍五入,但是如果我处理的数字低于 4 位,这会导致不良结果,因为我正在处理接近 0 的数字.它还会不时地抛出科学数字....在我的桌子上看起来不干净.

This has lead to me attempting to round before the highlighting, but that leads to undesirable results if I go lower than 4 places since I am working with numbers near 0. It'll also throw scientific numbers in from time to time....which just doesn't look clean in my 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

推荐答案

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

If I understand correctly what you want, this should work:

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

First let's convert your numeric columns into character vectors, with the required number of decimal places, using formatC

DF[, -1] = lapply(DF[, -1], formatC, format = 'f', flag='0', digits = 2)

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

Now we can apply the latex formatting using:

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

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

Note that paint still works on the character vectors, because the comparison to a numeric value coerces them to numeric.

这是一个完整的可重现示例,在实践中对此进行了演示

Here's a complete reproducible example, demonstrating this in practice

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}

这篇关于R 想要限制来自 csv 文件的位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆