elts: get elements from a consistently separated character string

View source: R/elts.R

eltsR Documentation

get elements from a consistently separated character string

Description

Another function that I keep rewriting; no more!

Usage

elts(x, sep = "_", elt = 1, naify = FALSE)

Arguments

x

the string(s) to split

sep

the separator (default is "_")

elt

the elements to get (default is element 1)

naify

return NA_character_ if y is not present? (FALSE)

Value

   a string, joined by `sep` if `elt` is a vector

Examples


string <- "foo_123_bar.baz.qux"

elts(string)                     # [1] "foo" 
elts(string, elts=2:3)           # [1] "123_bar.baz.qux"
elts(string, sep=".", elts=2:3)  # [1] "baz.qux"

strings <- paste0(letters[1:3], "_string")

elts(strings)              # a_string b_string c_string 
                           #      "a"      "b"      "c" 

strings2 <- c(strings, "nothing") 

elts(strings2)             #  a_string  b_string  c_string   nothing 
                           #       "a"       "b"       "c" "nothing" 
elts(strings2, naify=TRUE) #  a_string b_string c_string  nothing 
                           #       "a"      "b"      "c"       NA 


ttriche/sesamizeGEO documentation built on Nov. 12, 2023, 5:42 p.m.