6.3 Concatenate Strings
20180720 One of the most basic operations in string
manipulation is the concatenate operation. R provides alternatives for
doing so but a modern favourite is the %s+
operator from
stringi (Gagolewski et al. 2024).
## [1] "abcdefghi"
## [1] "abcmno" "defmno" "ghimno" "jklmno"
## [1] "abcmno" "defpqr" "ghimno" "jklpqr"
## [1] "abcmno" "defpqr" "ghistu" "jklvwx"
The tidy function for concatenating strings is
stringr::str_c(). A sep=
can be used to specify a
separator for the concatenated strings.
## [1] "helloworld"
## [1] "hello world"
We can also concatenate strings using glue::glue().
## helloworld
The traditional base::cat() function returns the concatenation of the supplied strings. Numeric and other complex objects are converted into character strings.
## hello world
## hello 123 world
Yet another alternative (and there are many) is the function backports::paste(). Notice that it separates the concatenated strings with a space.
## [1] "hello world"
References
Gagolewski, Marek, Bartek Tartanus, others; Unicode, Inc., et al. 2024. Stringi: Fast and Portable Character String Processing Facilities. https://stringi.gagolewski.com/.
Your donation will support ongoing availability and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984. Copyright © 1995-2022 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0