R TIBLES的数字打印格式

R TIBLES的数字打印格式,r,tibble,R,Tibble,我试图显示一个带有格式化数字的TIBLE,以便通过使用该数据类型的常用格式样式来简化表的读取 最理想的情况是,我在ggplot2的包行中搜索一些内容,以便可以执行以下操作: t <- tibble( surface = c(98000, 178000000000, 254000000), price = c(517244, 939484, 1340612), rate = c(0.12, 0.07, 0.045) ) print(t, label = c

我试图显示一个带有格式化数字的TIBLE,以便通过使用该数据类型的常用格式样式来简化表的读取

最理想的情况是,我在ggplot2的包行中搜索一些内容,以便可以执行以下操作:

t <- tibble(
    surface = c(98000, 178000000000, 254000000), 
    price = c(517244, 939484, 1340612), 
    rate = c(0.12, 0.07, 0.045)
)
print(t,
    label = c(
        surface = label_number_si(),
        price = label_dollar(),
        rate = label_percent()
    )
)
# A tibble: 3 x 3
    surface   price    rate
     <dbl>    <dbl>    <dbl>
1      98k $  517 244  12.0% 
2     178B $  939 484   7.0% 
3     254M $1 340 612   4.5%

目前,在打印TIBLE时,我收到以下输出,这很难阅读,尤其是价格栏:

print(t)
# A tibble: 3 x 3
       surface   price  rate
         <dbl>   <dbl> <dbl>
1        98000  517244 0.12 
2 178000000000  939484 0.07 
3    254000000 1340612 0.045
发现的所有类似问题,例如或似乎都围绕着使用选项SCIPEN=xxx的科学符号,该选项实际上不允许根据需要定义输出

我还尝试寻找其他软件包,例如,但这些软件包也不提供特定的数字格式,只将单位附加到列类型。

您可以使用scales::dollar设置价格格式,使用sprintf设置费率,使用助手函数设置我借用的surface的格式

图书馆弹琴 t=1e9~0浆糊压缩X,9,B, x>=1e6~0浆糊压缩x,6,M, x>=1000~0浆糊压缩x,3,k, x>=1~as.charactercompressx,0 } t2% 变异 表面=si_数表面,3, 价格=比例:美元价格, 比率=sprintf%.1f%%,比率*100 t2 >一个tibble:3x3 >表面价格率 > >19.8万美元517244 12.0% >21.78亿美元939484 7.0% >325400万美元1340612 4.5% 由v0.3.0于2020年2月24日创建,您可以使用scales::dollar设置价格格式,使用sprintf设置费率,使用辅助函数设置我借用的surface的格式

图书馆弹琴 t=1e9~0浆糊压缩X,9,B, x>=1e6~0浆糊压缩x,6,M, x>=1000~0浆糊压缩x,3,k, x>=1~as.charactercompressx,0 } t2% 变异 表面=si_数表面,3, 价格=比例:美元价格, 比率=sprintf%.1f%%,比率*100 t2 >一个tibble:3x3 >表面价格率 > >19.8万美元517244 12.0% >21.78亿美元939484 7.0% >325400万美元1340612 4.5%
由v0.3.0于2020年2月24日创建更改打印TIBLE格式的最简单方法是创建一个函数,用于打印TIBLE的变异版本

您可以使用一点非标准求值来传递要应用于每个列的任何函数。这与你想要的非常接近我认为:

图书馆管理员 图书馆天平
format_tibble更改打印的tibble格式的最简单方法是创建一个函数来打印该tibble的变异版本

您可以使用一点非标准求值来传递要应用于每个列的任何函数。这与你想要的非常接近我认为:

图书馆管理员 图书馆天平
通过将数据作为字符向量进行处理来格式化兼容解决方法:

library(tibble)

options(scipen = 12)

t <- tibble(
  surface = c(98000, 178000000000, 254000000), 
  price = c(517244, 939484, 1340612), 
  rate = c(0.12, 0.07, 0.045)
)
# temp vars
t$KMB <- ifelse(t$surface >= 10^3 & t$surface < 10^6, "K",
  ifelse(t$surface >= 10^6 & t$surface < 10^9, "M", "B"))
t$surface_char <- gsub("0", "", as.character(t$surface))

# paste elements together
t$surface <- paste0(t$surface_char, t$KMB)        
t$price <- paste0("$ ", t$price)
t$rate <- paste0(as.character(format(t$rate *100, nsmall = 1)), "%")

# remove temp vars
t$KMB <- NULL
t$surface_char <- NULL

print(t)

通过将数据作为字符向量进行处理来解决此问题:

library(tibble)

options(scipen = 12)

t <- tibble(
  surface = c(98000, 178000000000, 254000000), 
  price = c(517244, 939484, 1340612), 
  rate = c(0.12, 0.07, 0.045)
)
# temp vars
t$KMB <- ifelse(t$surface >= 10^3 & t$surface < 10^6, "K",
  ifelse(t$surface >= 10^6 & t$surface < 10^9, "M", "B"))
t$surface_char <- gsub("0", "", as.character(t$surface))

# paste elements together
t$surface <- paste0(t$surface_char, t$KMB)        
t$price <- paste0("$ ", t$price)
t$rate <- paste0(as.character(format(t$rate *100, nsmall = 1)), "%")

# remove temp vars
t$KMB <- NULL
t$surface_char <- NULL

print(t)

我认为kableExtra通常是你会看到的第一个软件包。我认为kableExtra通常是你会看到的第一个软件包。到目前为止,我最喜欢你的解决方案,它已经非常有用了。我唯一担心的是,使用您的方法,所有tibble现在看起来都像是只掌握了字符串。另外,使用更复杂的tibble,如libraryunits、tidyverse、scales,安装_symbolic_unitUSD t@Ben,这只是因为您无法将这些特定功能与您在tibble中创建的特定类型一起使用。你可以定义你喜欢的任何函数,以你选择的任何方式显示这些特定类型。到目前为止,我最喜欢你的解决方案,它已经非常有用了。我唯一担心的是,使用您的方法,所有tibble现在看起来都像是只掌握了字符串。另外,使用更复杂的tibble,如libraryunits、tidyverse、scales,安装_symbolic_unitUSD t@Ben,这只是因为您无法将这些特定功能与您在tibble中创建的特定类型一起使用。您可以定义任何函数,以便以您选择的任何方式显示这些特定类型。
library(tibble)

options(scipen = 12)

t <- tibble(
  surface = c(98000, 178000000000, 254000000), 
  price = c(517244, 939484, 1340612), 
  rate = c(0.12, 0.07, 0.045)
)
# temp vars
t$KMB <- ifelse(t$surface >= 10^3 & t$surface < 10^6, "K",
  ifelse(t$surface >= 10^6 & t$surface < 10^9, "M", "B"))
t$surface_char <- gsub("0", "", as.character(t$surface))

# paste elements together
t$surface <- paste0(t$surface_char, t$KMB)        
t$price <- paste0("$ ", t$price)
t$rate <- paste0(as.character(format(t$rate *100, nsmall = 1)), "%")

# remove temp vars
t$KMB <- NULL
t$surface_char <- NULL

print(t)