• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

语法

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

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

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

  • x - 为向量输入
  • digits - 是显示总位数
  • nsmall - 是最小位数的小数点右边
  • scientific - 设置为TRUE,则显示科学记数法
  • width - 指示要通过填充空白在开始时显示的最小宽度
  • justify - 是字符串显示在左边,右边或中心

示例

# 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  "

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
R语言:画树图发布时间:2022-07-18
下一篇:
R语言可视化学习笔记之添加p-value和显著性标记发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap