elts | R Documentation |
Another function that I keep rewriting; no more!
elts(x, sep = "_", elt = 1, naify = FALSE)
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) |
a string, joined by `sep` if `elt` is a vector
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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.