zoukankan      html  css  js  c++  java
  • R String 操作

    Strings

    1. Searching and replacing - grep, sub, gsub
    2. Creating strings from variables - sprintf, paste

    Creating strings from variables

    Table of contents

    o Creating strings from variables

    o Problem

    o Solution

    o Using paste()

    o Using sprintf()

    o Notes

    Problem

    You want to do create a string from variables.

    Solution

    The two common ways of creating strings from variables are the paste function and the sprintf function. pasteis more useful for vectors, and sprintf is more useful for precise control of the output.

    Using paste()

    a <- "apple"b <- "banana" 

    # Put a and b together, with a space in between:

    paste(a, b)

    # "apple banana"

     

    # With no space:

    paste(a, b, sep="")

    # "applebanana"

     

    # With a comma and space:

    paste(a, b, sep=", ")

    # "apple, banana"

     

    # With a vector

    d <- c("fig", "grapefruit", "honeydew") 

    # If the input is a vector, use collapse to put the elements together:

    paste(d, collapse=", ")

    # "fig, grapefruit, honeydew"

     

    # If the input is a scalar and a vector, it puts the scalar with each
    # element of the vector, and returns a vector:

    paste(a, d)

    # "apple fig"  "apple grapefruit"  "apple honeydew"  

     

    # Use sep and collapse:

    paste(a, d, sep="-", collapse=", ")

    # "apple-fig, apple-grapefruit, apple-honeydew"
    Using sprintf()

    Another way is to use sprintf function. This is derived from the function of the same name in the C programming language.

    To substitute in a string or string variable, use %s:

    a <- "string"sprintf("This is where a %s goes.", a)

    # "This is where a string goes."

    For integers, use %d or a variant:

    x <- 8sprintf("Regular:%d", x)

    # "Regular:8"

     

    # Can print to take some number of characters, leading with spaces.

    sprintf("Leading spaces:%4d", x)

    # "Leading spaces:   8"

     

    # Can also lead with zeros instead.

    sprintf("Leading zeros:%04d", x)

    #"Leading zeros:0008:"

    For floating-point numbers, use %f for standard notation, and %e or %E for exponential notation. You can also use %g or %G for a "smart" formatter that automatically switches between the two formats, depending on where the significant digits are. The following examples are taken from the R help page for sprintf:

    sprintf("%f", pi)         # "3.141593"sprintf("%.3f", pi)       # "3.142"sprintf("%1.0f", pi)      # "3"sprintf("%5.1f", pi)      # "  3.1"sprintf("%05.1f", pi)     # "003.1"sprintf("%+f", pi)        # "+3.141593"sprintf("% f", pi)        # " 3.141593"sprintf("%-10f", pi)      # "3.141593  "   (left justified)sprintf("%e", pi)         #"3.141593e+00"sprintf("%E", pi)         # "3.141593E+00"sprintf("%g", pi)         # "3.14159"sprintf("%g",   1e6 * pi) # "3.14159e+06"  (exponential)sprintf("%.9g", 1e6 * pi) # "3141592.65"   ("fixed")sprintf("%G", 1e-6 * pi)  # "3.14159E-06"

    In the %m.nf format specification: The m represents the field width, which is the minimum number of characters in the output string, and can be padded with leading spaces, or zeros if there is a zero in front of m. The nrepresents precision, which the number of digits after the decimal.

    Other miscellaneous things:

    sprintf("Substitute in multiple strings: %s %.5f", x, "string2")

    # "Substitute in multiple strings: string string2"

     

    # To print a percent sign, use "%%"

    sprintf("A single percent sign here %%")

    # "A single percent sign here %"
    tanhao2013@foxmail.com || http://weibo.com/buttonwood
  • 相关阅读:
    京东RPA:以企业数字化转型为驱动的机器人流程自动化解决方案专家
    运维大规模ES集群的思考和实践
    潘建伟团队再登Nature:建成全球首个集成量子通信网,全长4600公里
    数智化浪潮之中,传统企业如何抓住转型机遇?
    “持证”就能上岗 京东绿色内推招聘通道开启
    IoT爆发前夕,企业架构要面对哪些变革?
    如何使用ClickHouse实现时序数据管理和挖掘?
    图灵测试已过时,AI 需要新基准测试;别了Flash,Adobe播放器正式停运
    送你一份迷你书,全面了解如何做好大促技术备战
    Django 动态修改库名
  • 原文地址:https://www.cnblogs.com/buttonwood/p/2541334.html
Copyright © 2011-2022 走看看