• 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
  • 相关阅读:
    Spring.NET企业架构实践之 NHibernate + Spring.NET + WCF + Windows服务 + Silverlight 中小企业应用架构完整Demo
    UnitOfWork模式和Repository模式[转]
    倾情奉献:开源企业类库(EntLib)大分享
    Spring.NET企业架构实践之Nhibernate + WCF + ASP.NET MVC + NVelocity 对PetShop4.0重构(一)——架构设计
    WCF服务
    EA使用教程
    第三方支付接口,银行接口(附下载),third party bank interface
    ASP.NET 开发 WAP 网站
    开源软件Mono框架和架构
    wcf asp.net
  • 原文地址:https://www.cnblogs.com/buttonwood/p/2541334.html
Copyright © 2020-2023  润新知