我需要一个简单的函数或包格式:
1 6,000,000
2 75,000,400
3 743,450,000
4 340,000
5 4,300,000
要:
1 6.0 M
2 75.0 M
3 743.5 M
4 0.3 M
5 4.3 M
或以其他方式使大值(数百万,数十亿)更易于在表格中打印。
答案 0 :(得分:24)
这使用findInterval
来定义后缀并确定分母。如果想要低于1.0或高于1万亿,可以轻松地向任一方向扩展:
comprss <- function(tx) {
div <- findInterval(as.numeric(gsub("\\,", "", tx)),
c(0, 1e3, 1e6, 1e9, 1e12) )
paste(round( as.numeric(gsub("\\,","",tx))/10^(3*(div-1)), 2),
c("","K","M","B","T")[div] )}
如果输入为数字,则无需删除as.numeric或gsub。这无疑是多余的,但会成功。这是Grgor的例子的结果:
> comprss (big_x)
[1] "123 " "500 " "999 " "1.05 K" "9 K"
[6] "49 K" "105.4 K" "998 K" "1.5 M" "20 M"
[11] "313.4 M" "453.12 B"
使用原始输入(实际上是因子变量。)
comprss (dat$V2)
[1] "6 M" "75 M" "743.45 M" "340 K" "4.3 M"
当然,可以使用显式print
命令和quotes = FALSE或使用cat
打印不带引号的内容。
答案 1 :(得分:23)
如果您从此数字向量x
开始,
x <- c(6e+06, 75000400, 743450000, 340000, 4300000)
您可以执行以下操作。
paste(format(round(x / 1e6, 1), trim = TRUE), "M")
# [1] "6.0 M" "75.0 M" "743.5 M" "0.3 M" "4.3 M"
如果您不关心尾随零,请删除format()
来电。
paste(round(x / 1e6, 1), "M")
# [1] "6 M" "75 M" "743.5 M" "0.3 M" "4.3 M"
或者,您可以使用print方法分配S3类,并将y
保留为数字。在这里,我使用paste0()
使结果更清晰。
print.million <- function(x, quote = FALSE, ...) {
x <- paste0(round(x / 1e6, 1), "M")
NextMethod(x, quote = quote, ...)
}
## assign the 'million' class to 'x'
class(x) <- "million"
x
# [1] 6M 75M 743.5M 0.3M 4.3M
x[]
# [1] 6000000 75000400 743450000 340000 4300000
你也可以为数十亿甚至数万亿做同样的事情。有关如何将其置于数据框中的信息,请参阅this answer,因为您需要format()
和as.data.frame()
方法。
答案 2 :(得分:8)
另一种选择,从数字(而非字符)数字开始,适用于数百万和数十亿(及以下)。您可以将更多参数传递给formatC
以自定义输出,并在需要时扩展到Trillions。
m_b_format = function(x) {
b.index = x >= 1e9
m.index = x >= 1e5 & x < 1e9
output = formatC(x, format = "d", big.mark = ",")
output[b.index] = paste(formatC(x[b.index] / 1e9, digits = 1, format = "f"), "B")
output[m.index] = paste(formatC(x[m.index] / 1e6, digits = 1, format = "f"), "M")
return(output)
}
your_x = c(6e6, 75e6 + 400, 743450000, 340000, 43e6)
> m_b_format(your_x)
[1] "6.0 M" "75.0 M" "743.5 M" "0.3 M" "43.0 M"
big_x = c(123, 500, 999, 1050, 9000, 49000, 105400, 998000,
1.5e6, 2e7, 313402182, 453123634432)
> m_b_format(big_x)
[1] "123" "500" "999" "1,050" "9,000" "49,000"
[7] "0.1 M" "1.0 M" "1.5 M" "20.0 M" "313.4 M" "453.1 B"
答案 3 :(得分:8)
scales
软件包的最新版本包括打印可读标签的功能。如果您使用的是ggplot或tidyverse,则可能已经安装了scales
。不过,您可能必须更新软件包。
在这种情况下,可以使用label_number_si
:
> library(scales)
> inp <- c(6000000, 75000400, 743450000, 340000, 4300000)
> label_number_si(accuracy=0.1)(inp)
[1] "6.0M" "75.0M" "743.4M" "340.0K" "4.3M"
答案 4 :(得分:1)
借用其他答案并添加它们,主要目的是为ggplot2轴生成漂亮的标签。是的,只有正值(负值将保持不变),因为通常我只希望这些后缀用于正数量。容易扩展到负数。
# Format numbers with suffixes K, M, B, T and optional rounding. Vectorized
# Main purpose: pretty formatting axes for plots produced by ggplot2
#
# Usage in ggplot2: scale_x_continuous(labels = suffix_formatter)
suffix_formatter <- function(x, digits = NULL)
{
intl <- c(1e3, 1e6, 1e9, 1e12);
suffixes <- c('K', 'M', 'B', 'T');
i <- findInterval(x, intl);
result <- character(length(x));
# Note: for ggplot2 the last label element of x is NA, so we need to handle it
ind_format <- !is.na(x) & i > 0;
# Format only the elements that need to be formatted
# with suffixes and possible rounding
result[ind_format] <- paste0(
formatC(x[ind_format]/intl[i[ind_format]], format = "f", digits = digits)
,suffixes[i[ind_format]]
);
# And leave the rest with no changes
result[!ind_format] <- as.character(x[!ind_format]);
return(invisible(result));
}
使用示例。
x <- seq(1:10);
d <- data.frame(x = x, y = 10^x);
ggplot(aes(x=x, y=y), data = d) + geom_line() + scale_y_log10()
ggplot(aes(x=x, y=y), data = d) + geom_line() + scale_y_log10(labels = suffix_formatter)
答案 5 :(得分:1)
dplyr的casewhen
现在对此提供了更友好的解决方案-例如:
format_bignum = function(n){
case_when(
n >= 1e12 ~ paste(round(n/1e12), 'Tn'),
n >= 1e9 ~ paste(round(n/1e9), 'Bn'),
n >= 1e6 ~ paste(round(n/1e6), 'M'),
n >= 1e3 ~ paste(round(n/1e3), 'K'),
TRUE ~ as.character(n))
}
或者,您可以将case_when
位嵌入到mutate
调用中。
答案 6 :(得分:0)
我重写了@ 42-函数来容纳%数字,比如这个
compress <- function(tx) {
tx <- as.numeric(gsub("\\,", "", tx))
int <- c(1e-2, 1, 1e3, 1e6, 1e9, 1e12)
div <- findInterval(tx, int)
paste(round( tx/int[div], 2), c("%","", "K","M","B","T")[div] )
}
>tx
total_reads total_bases q20_rate q30_rate gc_content
3.504660e+05 1.051398e+08 6.648160e-01 4.810370e-01 5.111660e-01
> compress(tx)
[1] "350.47 K" "105.14 M" "66.48 %" "48.1 %" "51.12 %"
这对类似问题可能有用
答案 7 :(得分:0)
类似于@Alex Poklonskiy,我需要一个用于图表的格式化程序。但是我需要一个支持负数的版本。这是他调整后的功能(尽管我不是R编程专家):
number_format <- function(x, digits = NULL)
{
intl <- c(1e3, 1e6, 1e9, 1e12)
suffixes <- c(' K', ' M', ' B', ' T')
i <- findInterval(x, intl)
i_neg <- findInterval(-x, intl)
result <- character(length(x))
# Note: for ggplot2 the last label element of x is NA, so we need to handle it
ind_format <- !is.na(x) & i > 0
neg_format <- !is.na(x) & i_neg > 0
# Format only the elements that need to be formatted
# with suffixes and possible rounding
result[ind_format] <- paste0(
formatC(x[ind_format] / intl[i[ind_format]], format = "f", digits = digits),
suffixes[i[ind_format]]
)
# Format negative numbers
result[neg_format] <- paste0(
formatC(x[neg_format] / intl[i_neg[neg_format]], format = "f", digits = digits),
suffixes[i_neg[neg_format]]
)
# To the rest only apply rounding
result[!ind_format & !neg_format] <- as.character(
formatC(x[!ind_format & !neg_format], format = "f", digits = digits)
)
return(invisible(result))
}
我还调整了digits
参数用于舍入没有后缀的值(例如1.23434546
)
用法示例:
> print( number_format(c(1.2325353, 500, 132364584563, 5.67e+9, -2.45e+7, -1.2333, -55)) )
[1] "1.2325" "500.0000" "132.3646 B" "5.6700 B" "-24.5000 M" "-1.2333" "-55.0000"
> print( number_format(c(1.2325353, 500, 132364584563, 5.67e+9, -2.45e+7, -1.2333, -55), digits = 2) )
[1] "1.23" "500.00" "132.36 B" "5.67 B" "-24.50 M" "-1.23" "-55.00"
答案 8 :(得分:0)
带有$token= & az account get-access-token --resource=https://database.windows.net --query accessToken
Write-Host("##vso[task.setvariable variable=sqlToken]$token")
软件包的另一种选择是使用scales
:
unit_format