如何在kableExtra中为整数打印小数点

huangapple go评论49阅读模式
英文:

How to print decimal points for integers in kableExtra

问题

我在R中有以下的数据框。我尝试使用kableExtra创建一个表格,是否有办法在表格中打印整数的小数点为零(以便我的数据框中的7被打印为7.0)?我只希望在打印表格时,表格中的所有数字都具有一致的小数点。

data <- structure(list(Model = c("ARIMA", "Actual", "Forecast", "Error", 
"STIF", "Actual", "Forecast", "Error", "Adjusted Forecast", "Actual", 
"Forecast", "Error"), `*Dec 2022` = c(NA, 6.9, 6.8, 0.1, NA, 6.9, 6.6, 0.3, NA, 6.9, 6.7, 0.2), 
`*Jan 2023` = c(NA, 7, 6.8, 0.2, NA, 7, 6.8, 0.2, NA, 7, 6.9, 0.1), 
`*Feb 2023` = c(NA, 7.2, 6.2, 1, NA, 7.2, 6.9, 0.3, NA, 7.2, 6.6, 0.6), 
Average = c(NA, 7, 6.6, 0.4, NA, 7, 6.8, 0.3, NA, 7, 6.7, 0.3)), row.names = c(NA, -12L), 
class = c("tbl_df", "tbl", "data.frame"))

library(kableExtra)
library(tidyverse)

data %>% 
  mutate_if(is.numeric, ~as.character(.)) %>% 
  replace(is.na(.), " ") %>% 
  kable(caption="Short-Term Forecast Errors \\label{Table6}", booktabs = T, escape = FALSE, align = 'lcccc') %>% 
  kable_styling(latex_options = c("HOLD_position"), font_size = 9)

如何在kableExtra中为整数打印小数点


注意:上述代码段已经包括在原文中,它包含了如何在表格中打印整数的小数点为零的步骤。

<details>
<summary>英文:</summary>

I have the following dataframe in R. I&#39;m trying to create a table using ```kableExtra```, is there a way to print the zero decimal point for the integers in the table (so that the 7&#39;s in my dataframe are printed as 7.0 instead)? I would just like all of the numbers in my table to have consistent decimal points when I print my table

data <- structure(list(Model = c("ARIMA", "Actual", "Forecast", "Error",
"STIF", "Actual", "Forecast", "Error", "Adjusted Forecast", "Actual",
"Forecast", "Error"), *Dec 2022 = c(NA, 6.9, 6.8, 0.1, NA,
6.9, 6.6, 0.3, NA, 6.9, 6.7, 0.2), *Jan 2023 = c(NA, 7, 6.8,
0.2, NA, 7, 6.8, 0.2, NA, 7, 6.9, 0.1), *Feb 2023 = c(NA, 7.2,
6.2, 1, NA, 7.2, 6.9, 0.3, NA, 7.2, 6.6, 0.6), Average = c(NA,
7, 6.6, 0.4, NA, 7, 6.8, 0.3, NA, 7, 6.7, 0.3)), row.names = c(NA,
-12L), class = c("tbl_df", "tbl", "data.frame"))

library(kableExtra)
library(tidyverse)

data %>%
mutate_if(is.numeric, ~as.character(.)) %>%
replace(is.na(.), " ") %>%
kable(caption="Short-Term Forecast Errors \label{Table6}", booktabs = T, escape = FALSE, align = 'lcccc') %>%
kable_styling(latex_options = c("HOLD_position"), font_size = 9)


[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/WMWxE.png

</details>


# 答案1
**得分**: 2

To display the same number of decimal points in the table, you can use the `format()` function. Your code may look like this:

```r
data %>%
  mutate_if(is.numeric, ~ifelse(is.na(.), " ", format(round(., 1)))) %>%
  kable(caption="Short-Term Forecast Errors \\label{Table6}", booktabs = T, escape = FALSE, align = 'lcccc') %>%
  kable_styling(latex_options = c("HOLD_position"), font_size = 9)

The round() function is used to round the numeric values to one decimal place before formatting.

英文:

To display the same number of decimal points in the table, you can use the format() function. Your code may look like this :

data %&gt;% 
  mutate_if(is.numeric, ~ifelse(is.na(.), &quot; &quot;, format(round(., 1)))) %&gt;%
  kable(caption=&quot;Short-Term Forecast Errors \\label{Table6}&quot;, booktabs = T, escape = FALSE, align = &#39;lcccc&#39;) %&gt;% 
  kable_styling(latex_options = c(&quot;HOLD_position&quot;), font_size = 9)

The round() function is used to round the numeric values to one decimal places before formatting.

huangapple
  • 本文由 发表于 2023年4月4日 16:15:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定