zoukankan      html  css  js  c++  java
  • 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  "
  • 相关阅读:
    HDU5269 字典树
    HDU1664 BFS + 数论 + 剪枝
    HDU1429 BFS + 状态压缩
    HDU1075 字典树 + 字符串映射
    HDU1247 字典树
    UVa 10256(凸包、线段交、点在多边形内)
    UVa 10652(旋转、凸包、多边形面积)
    牛客练习赛43D(贪心)
    牛客练习赛43F(推式子)
    Codeforces 1161B(判断旋转对称)
  • 原文地址:https://www.cnblogs.com/csguo/p/7294080.html
Copyright © 2011-2022 走看看