数字和字符串可以使用 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 "

R语言格式化数字和字符串format函数的更多相关文章

  1. C语言把数字转换为字符串的函数

    博主原文 C语言itoa()函数和atoi()函数详解(整数转字符C实现) C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. 1.int/float to st ...

  2. R语言 格式化数字

    x = 1111111234.6547389758965789345 y = formatC(x, digits = 8, format = "f") # [1] "11 ...

  3. Python 的格式化字符串format函数

    阅读mattkang在csdn中的博客<飘逸的python - 增强的格式化字符串format函数>所做笔记 自从python2.6开始,新增了一种格式化字符串的函数str.format( ...

  4. R语言:文本(字符串)处理与正则表达式

    R语言:文本(字符串)处理与正则表达式 (2014-03-27 16:40:44) 转载▼ 标签: 教育 分类: R 处理文本是每一种计算机语言都应该具备的功能,但不是每一种语言都侧重于处理文本.R语 ...

  5. python字符串格式化方法%s和format函数

    1.%s方法 一个例子 print("my name is %s and i am %d years old" %("xiaoming",18) 输出结果:my ...

  6. 飘逸的python - 增强的格式化字符串format函数

    自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱. 语法 它通过{}和 ...

  7. 格式化字符串format函数

    自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱. 语法 它通过{}和 ...

  8. (转)飘逸的python - 增强的格式化字符串format函数

    原文:https://blog.csdn.net/handsomekang/article/details/9183303 Python字符串格式化--format()方法-----https://b ...

  9. 增强的格式化字符串format函数

    http://blog.csdn.net/handsomekang/article/details/9183303 自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓 ...

随机推荐

  1. leetcode 有效的括号

    给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: - 左括号必须用相同类型的右括号闭合. - 左括号必须以正确的顺序闭合. 注意空字符 ...

  2. k8s 常用指令

    kubectl rolling-update redis-master --image=redis-master:2.0 基于镜像灰度发布 推荐链接

  3. CF834D The Bakery

    题目链接:戳我 题意:将一个长度为n的序列分为k段,使得总价值最大.一段区间的价值表示为区间内不同数字的个数 \(n<=35000,k<=50\) 开始想的转移方程是这个样子的--\(dp ...

  4. Mounting VMDK files in Linux

    1.用 loop 方式挂载 vmdk 文件 losetup /dev/loop0 docker_pull-flat.vmdk 2.查看分区 [root@localhost]# parted /dev/ ...

  5. 如何让Gogland不过期,一直使用?

    Gogland是jetBrains公司出品的GO语言开发IDE,是目前最好的GO语言开发工具!!但是目前Gogland提供的试用版,有一定的使用期限,如何到期还能使用?经过我的测试,如果Gogland ...

  6. PHP设计超级好用的文件上传处理类一 (37)

    <?php class FileUpload { private $filepath; //指定上传文件保存的路径 private $allowtype=array('gif', 'jpg', ...

  7. docker安装配置

    ########################################## #docker安装配置 #环境centos7 #配置docker阿里源 echo '#Docker [docker ...

  8. linux将指令加入开机启动或加入环境变量

    以mongodb运行指令为例,/usr/local/webserver/mongodb/bin/mongo 1,linux将指令加入环境变量PATH 简单说PATH就是一组路径的字符串变量,当你输入的 ...

  9. Word2007文档中怎么输入上标下标

    1.Word中输出Z = X2 + Y2 此公式流程: 首先在Word中写入:Z = X2 + Y2: 方法1:选中X后面的2,再按组合键“Ctrl+Shift+加号键”即可,如此操作Y后面的2即可.

  10. MongoDB健壮集群——用副本集做分片

    1.    MongoDB分片+副本集 健壮的集群方案 多个配置服务器 多个mongos服务器  每个片都是副本集 正确设置w 架构图 说明: 1.   此实验环境在一台机器上通过不同port和dbp ...