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(),可谓 ...
随机推荐
- Zero Clipboard js+swf实现的复制功能使用方法
开发中经常会用到复制的功能,在 IE 下实现比较简单.但要想做到跨浏览器比较困难了.本文将介绍一个跨浏览器的库类 Zero Clipboard .它利用 Flash 进行复制,所以只要浏览器装有 Fl ...
- pipeline构建时报错问题解决
问题: 1.No such field found: field java.lang.String sh. Administrators can decide whether to approve o ...
- AutoMapper在C#中的有趣应用
最近发现了一个比较有趣的东西 AutoMapper,主要将Model转换为DTO,DTO更注重数据,对领域对象进行合理封装,从而不会将领域对象的行为过分暴露给表现层. 先来看一点实例,两个类之间的映射 ...
- .Net MVC5路由机制与扩展
新建一个MVC项目启动后,首先访问的地址是http://localhost:xxx/Home/Index,这时候我们也明白因为在程序中有个叫做Home的控制器,并且在这个控制器下面有个叫做Index的 ...
- how to remote debug in vs 2013
first download the debugger tools "rtools_setup_x64" start C:\Program Files\Microsoft Visu ...
- iOS 访问模拟器数据
1. 如果文件隐藏,进入命令行,输入: defaults write com.apple.finder AppleShowAllFiles -bool true 2. 进入 Finder,shift ...
- python反转列表的几种方法
一.使用reversed()函数 a = [1, 2, 3, 4] b = list(reversed(a)) 注意:reversed()函数返回的是一个迭代器,而不是一个List,需要再使用List ...
- AssertionError: View function mapping is overwriting an existing endpoint function: admin.main
刚才给views.py文件添加了一个路由地址: @admin_view.route('/test', methods=["get", "post"]) @log ...
- mxonline实战5,用户注册的验证码
github对应地址:验证码好麻烦 一. 安装 配置 1. pip install django-simple-captcha 2. add captcha to the INSTAL ...
- JSP里面九个内置对象
JSP内置对象(9个常用的内置对象) 1.request对象 客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求, 然后做出响应.它是HttpServletRequest类的实例 ...