R语言格式化数字和字符串format函数
数字和字符串可以使用 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函数的更多相关文章
- C语言把数字转换为字符串的函数
博主原文 C语言itoa()函数和atoi()函数详解(整数转字符C实现) C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. 1.int/float to st ...
- R语言 格式化数字
x = 1111111234.6547389758965789345 y = formatC(x, digits = 8, format = "f") # [1] "11 ...
- Python 的格式化字符串format函数
阅读mattkang在csdn中的博客<飘逸的python - 增强的格式化字符串format函数>所做笔记 自从python2.6开始,新增了一种格式化字符串的函数str.format( ...
- R语言:文本(字符串)处理与正则表达式
R语言:文本(字符串)处理与正则表达式 (2014-03-27 16:40:44) 转载▼ 标签: 教育 分类: R 处理文本是每一种计算机语言都应该具备的功能,但不是每一种语言都侧重于处理文本.R语 ...
- python字符串格式化方法%s和format函数
1.%s方法 一个例子 print("my name is %s and i am %d years old" %("xiaoming",18) 输出结果:my ...
- 飘逸的python - 增强的格式化字符串format函数
自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱. 语法 它通过{}和 ...
- 格式化字符串format函数
自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱. 语法 它通过{}和 ...
- (转)飘逸的python - 增强的格式化字符串format函数
原文:https://blog.csdn.net/handsomekang/article/details/9183303 Python字符串格式化--format()方法-----https://b ...
- 增强的格式化字符串format函数
http://blog.csdn.net/handsomekang/article/details/9183303 自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓 ...
随机推荐
- [Oracle]Oracle部分函数
1.nvl(a,b) 若a为null,则b 2.to_char(date,'YY-MM-DD') 按格式将date类型转为字符串 to_date('1999/1/1','YY-MM-DD') 将字符串 ...
- NIO-直接内存
写NIO程序经常使用ByteBuffer来读取或者写入数据,那么使用ByteBuffer.allocate(capability)还是使用ByteBuffer.allocteDirect(capabi ...
- 使用X-Pack插件来进行权限控制
1.为elasticsearch安装X-Pack插件.进入 elasticsearch根目录,执行: bin/elasticsearch-plugin install x-pack杀掉进程,重启es, ...
- linux中文件句柄数问题
问题描述: 有时候业务比较繁忙时,就会出现如下问题 too many open files:顾名思义即打开过多文件数.不过这里的files不单是文件的意思,也包括打开的通讯链接(比如sock ...
- D02——C语言基础学PYTHON
C语言基础学习PYTHON——基础学习D02 20180801内容纲要: 1 字符串的系列操作 2 集合 3 文件的读写 4 字符编码转换 5 小结 6 练习:三级菜单(有彩蛋) 1 字符串的系列操 ...
- open/read/write/close
open 函数 函数原型 #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int o ...
- appium获取toast方法
配置toast请注意: 1.指定desired_caps["automationName"] = "UiAutomator2" 2.要求安装jdk1.8 64位 ...
- EDEM 2018 + Fluent 19.0耦合
具体步骤参考流沙的文章即可,如果python版本较高,可能有个地方需要小小的改动一下: tools文件夹下的compile_lib_edem_coupling.py文件中,导入模块有个地方需要修改 其 ...
- java ListNode链表数据结构
class ListNode{ int val; ListNode next; } 该节点的值 val. 下一个节点 next
- ubuntu 16.04安装后的简单优化
1.更换更新源为国内源: sudo vim /etc/apt/sources.list vim 打开更新源配置文件添加国内源进去,这里添加阿里源 deb http://mirrors.aliyun.c ...