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
  • 相关阅读:
    201521123100《Java程序设计》第八周学习总结
    201521123100 《Java程序设计》 第7周学习总结
    201521123100 《Java程序设计》第6周学习总结
    201521123100 《Java程序设计》第5周学习总结
    201521123100 《Java程序设计》第4周学习总结
    201521123100 《Java程序设计》第3周学习总结
    201521123093 java 第十一周学习总结
    201521123093 java 第十周学习总结
    201521123093 java 第九周学习总结
    201521123093 java 第八周总结
  • 原文地址:https://www.cnblogs.com/buttonwood/p/2541334.html
Copyright © 2011-2022 走看看