R语言格式化数字和字符串format函数

时间:2017-08-06 11:55:09   收藏:0   阅读:1733

数字和字符串可以使用 format()函数的格式化为特定样式。

语法

format()函数的基本语法是:

format(x, digits, nsmall,scientific,width,justify = c("left", "right", "centre", "none"))     

以下是所使用的参数的说明:

示例

# Total number of digits displayed. Last digit rounded off.
result <- format(23.123456789, digits = 9)
print(result)

# Display numbers in scientific notation.
result <- format(c(6, 13.14521), scientific = TRUE)
print(result)

# The minimum number of digits to the right of the decimal point.
result <- format(23.47, nsmall = 5)
print(result)

# Format treats everything as a string.
result <- format(6)
print(result)

# Numbers are padded with blank in the beginning for width.
result <- format(13.7, width = 6)
print(result)

# Left justify strings.
result <- format("Hello",width = 8, justify = "l")
print(result)

# Justfy string with center.
result <- format("Hello",width = 8, justify = "c")
print(result)

当我们上面的代码执行时,它产生以下结果:

[1] "23.1234568"
[1] "6.000000e+00" "1.314521e+01"
[1] "23.47000"
[1] "6"
[1] "  13.7"
[1] "Hello   "
[1] " Hello  "

原文:http://www.cnblogs.com/csguo/p/7294080.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!