【问题标题】:creating a nice table in R and using the format() function to create digits在 R 中创建一个漂亮的表并使用 format() 函数创建数字
【发布时间】:2021-05-25 09:02:54
【问题描述】:

我编写了以下代码来在 R 中创建一个表。 但是,我似乎无法让 format() 数字命令工作。 我有一个特定的列"Population Mean",我想用数字“,”分隔数字。

'''

knitr::kable(mean_pop_1, "pipe", 
col.name=c("Country", "Population Mean"), 
align = c("l", "c"), 
caption = "Top 10 Countries Mean Population", format(","))

我收到此错误:

回合错误(x[, j], digits[j]): 数学函数的非数字参数 '''

数据框:

country       Population_mean
   <fct>                   <dbl>
 1 China              958160052.
 2 India              701130740.
 3 United States      228211232.
 4 Indonesia          148322833.
 5 Brazil             122312127.
 6 Japan              111758808 
 7 Pakistan            93683386.
 8 Bangladesh          90755395.
 9 Germany             77547043.
10 Nigeria             73708018.

这里是上面例子的代码:

df <- structure(list(Country = c("China", "India", "United States", 
                      "Indonesia", "Brazil", "Japan", "Pakistan", 
                       "Bangladesh", "Germany", "Nigeria"), 
                      Population_mean = c(958160052, 701130740, 
                       228211232, 148322833, 122312127, 111758808, 
                       93683386, 90755395, 77547043, 73708018)), 
                 row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))

【问题讨论】:

  • 您能否提供一些可重复的数据集,这将帮助 SO 社区为您找到最佳解决方案。
  • 嗨 :-),我添加了一些数据。 “Country”和“Population_mean”是标题
  • 嗨,玛丽亚,谢谢。这可能有助于解决下一个问题stackoverflow.com/help/how-to-ask。当我问一个可重现的小例子时,我指的是使用“dput”命令,如下所示:stackoverflow.com/questions/49994249/example-of-using-dput,我会尽快编辑您的问题。
  • 每天学习 - 感谢您查看我的问题:-D
  • @LucaCoding - 它上面代码中的“格式”功能给我带来了一些麻烦。我需要在“人口平均值”列中使用一千个分隔符

标签: r datatable rounding


【解决方案1】:
knitr::kable(mean_pop_1, "pipe", format.args = list(big.mark = ","),
   col.name=c("Country", "Population Mean"), 
   align = c("l", "r"), caption = "Top 10 Countries Mean Population")

【讨论】:

  • 请不要只发布“仅代码”的答案。提供一些见解和解释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-09
  • 2013-01-02
  • 2020-09-04
  • 1970-01-01
  • 2020-06-19
  • 2021-11-11
相关资源
最近更新 更多