splitString {cwhstring}R Documentation

Split a string at the location(s) of a delimiting string, at the single characters of a delimiter

Description

splitString splits string at the location(s) of a delimiting string getSubs splits string at the location(s) of the constituent characters of a delimiting string cv Convert string to vector of characters. vc Convert vector of characters to string.

Usage

  splitString(str,pattern,clean=FALSE)
  getSubs(str, sep = ",")
  cv(str)
  vc(vec)

Arguments

str A string.
pattern The pattern acting as the delimiting string.
clean If TRUE then elements "" will be discarded.
sep A vector of characters or a string.
vec A vector of characters.

Value

splitString A character vector of the parts. If clean == TRUE then elements containing "" are omitted.
getSubs A character vector of the parts.
cv a vector of characters.
vc a string.

Note

Uses substring, substring.location.

Author(s)

John Wallace, rw@fish.washington.edu, (cv, vc:) Christian W. Hoffmann, christian.hoffmann@wsl.ch, http://www.wsl.ch/staff/christian.hoffmann

Examples

nj.string <- " 12.345   1.2345E+02  0.123"
splitString(nj.string," ",clean=TRUE)
  # [1] "12.345"     "1.2345E+02" "0.123"
splitString(nj.string,"3",clean=TRUE)
  # [1] " 12."         "45   1.2"     "45E+02  0.12"

as.numeric(getSubs(" 12.345   1.2345E+02  0.123", sep=" ")) # [1]  12.345 123.450   0.123

rev(getSubs("I want to grab this number as is 4E+03", " "))[1] #  "4E+03"

#For a vector of character strings, with the same number of elements in each string use:

x <- c(" 12.345   1.2345E+02  0.123"," 2 4.56   5.67")
c(apply(matrix(x), 1, getSubs, " "))
# [1] "12.345"     "1.2345E+02" "0.123"  "2"    "4.56"   "5.67" 

#else use:

y <- c(" 12.345   1.2345E+02  0.123", " 5 3.56")                
unlist(apply(matrix(y), 1, getSubs, " "))
#  "12.345"     "1.2345E+02" "0.123"      "5"          "3.56"   

cv("I love you")  # [1] "I" " " "l" "o" "v" "e" " " "y" "o" "u"
vc(rev(cv("I love you"))) # "uoy evol I"

[Package cwhstring version 1.0.0 Index]