如何将数字格式化为R中的百分比?

令我困惑的R之一是如何格式化数字以百分比形式打印。


例如,显示0.12345为12.345%。我有很多解决方法,但是这些似乎都不是“ newby friendly”。例如:


set.seed(1)

m <- runif(5)


paste(round(100*m, 2), "%", sep="")

[1] "26.55%" "37.21%" "57.29%" "90.82%" "20.17%"


sprintf("%1.2f%%", 100*m)

[1] "26.55%" "37.21%" "57.29%" "90.82%" "20.17%"

问题:是否有基本的R函数可以执行此操作?或者,是否有使用广泛的包装提供方便的包装?


尽管寻找的东西,像这样的?format,?formatC而且?prettyNum,我还没有找到合适方便的包装在基地R. ??"percent"没有产生任何有用的东西。 library(sos); findFn("format percent")返回1250次匹配-再次没有用。 ggplot2具有功能,percent但无法控制舍入精度。


holdtom
浏览 3462回答 3
3回答

HUH函数

几年后的更新:这些天percent,scales软件包中已经有一个功能,如krlmlr的答案所述。使用它代替我的手动解决方案。尝试类似percent <- function(x, digits = 2, format = "f", ...) {&nbsp; paste0(formatC(100 * x, format = format, digits = digits, ...), "%")}随着使用,例如x <- c(-1, 0, 0.1, 0.555555, 1, 100)percent(x)(如果您愿意,请将格式从更改"f"为"g"。)

幕布斯7119047

签出scales包装。ggplot2我认为它曾经是的一部分。library('scales')percent((1:10) / 100)#&nbsp; [1] "1%"&nbsp; "2%"&nbsp; "3%"&nbsp; "4%"&nbsp; "5%"&nbsp; "6%"&nbsp; "7%"&nbsp; "8%"&nbsp; "9%"&nbsp; "10%"在大多数情况下,用于检测精度的内置逻辑应该可以很好地工作。percent((1:10) / 1000)#&nbsp; [1] "0.1%" "0.2%" "0.3%" "0.4%" "0.5%" "0.6%" "0.7%" "0.8%" "0.9%" "1.0%"percent((1:10) / 100000)#&nbsp; [1] "0.001%" "0.002%" "0.003%" "0.004%" "0.005%" "0.006%" "0.007%" "0.008%"#&nbsp; [9] "0.009%" "0.010%"percent(sqrt(seq(0, 1, by=0.1)))#&nbsp; [1] "0%"&nbsp; &nbsp;"32%"&nbsp; "45%"&nbsp; "55%"&nbsp; "63%"&nbsp; "71%"&nbsp; "77%"&nbsp; "84%"&nbsp; "89%"&nbsp; "95%"&nbsp;# [11] "100%"percent(seq(0, 0.1, by=0.01) ** 2)#&nbsp; [1] "0.00%" "0.01%" "0.04%" "0.09%" "0.16%" "0.25%" "0.36%" "0.49%" "0.64%"# [10] "0.81%" "1.00%"

慕桂英546537

我做了一些基准测试对这些问题的答案的速度和惊讶地看到percent在scales如此吹捧包装,鉴于其疲弱。我想它的优势是它的自动检测器可以正确格式化,但是如果您知道数据看起来像什么,那么显然可以避免。以下是尝试将(0,1)中的100,000个百分比的列表格式设置为2位数字的百分比的结果:library(microbenchmark)x = runif(1e5)microbenchmark(times = 100L, andrie1(), andrie2(), richie(), krlmlr())# Unit: milliseconds#&nbsp; &nbsp;expr&nbsp; &nbsp; &nbsp; &nbsp;min&nbsp; &nbsp; &nbsp; &nbsp; lq&nbsp; &nbsp; &nbsp; mean&nbsp; &nbsp; median&nbsp; &nbsp; &nbsp; &nbsp; uq&nbsp; &nbsp; &nbsp; &nbsp;max# 1 andrie1()&nbsp; 91.08811&nbsp; 95.51952&nbsp; 99.54368&nbsp; 97.39548 102.75665 126.54918 #paste(round())# 2 andrie2()&nbsp; 43.75678&nbsp; 45.56284&nbsp; 49.20919&nbsp; 47.42042&nbsp; 51.23483&nbsp; 69.10444 #sprintf()# 3&nbsp; richie()&nbsp; 79.35606&nbsp; 82.30379&nbsp; 87.29905&nbsp; 84.47743&nbsp; 90.38425 112.22889 #paste(formatC())# 4&nbsp; krlmlr() 243.19699 267.74435 304.16202 280.28878 311.41978 534.55904 #scales::percent()因此sprintf,当我们要添加百分号时,它将成为明显的赢家。另一方面,如果我们只想将数字乘以四舍五入(从比例乘以百分比而没有“%”,则round()最快):# Unit: milliseconds#&nbsp; &nbsp; &nbsp; &nbsp; expr&nbsp; &nbsp; &nbsp; min&nbsp; &nbsp; &nbsp; &nbsp; lq&nbsp; &nbsp; &nbsp; mean&nbsp; &nbsp; median&nbsp; &nbsp; &nbsp; &nbsp; uq&nbsp; &nbsp; &nbsp; &nbsp;max# 1 andrie1()&nbsp; 4.43576&nbsp; 4.514349&nbsp; 4.583014&nbsp; 4.547911&nbsp; 4.640199&nbsp; 4.939159 # round()# 2 andrie2() 42.26545 42.462963 43.229595 42.960719 43.642912 47.344517 # sprintf()# 3&nbsp; richie() 64.99420 65.872592 67.480730 66.731730 67.950658 96.722691 # formatC()
打开App,查看更多内容
随时随地看视频慕课网APP