Data Science Desktop Survival Guide
by Graham Williams |
|||||
Concatenate Strings |
"abc" %s+% "def" %s+% "ghi"
c("abc", "def", "ghi", "jkl") %s+% c("mno")
c("abc", "def", "ghi", "jkl") %s+% c("mno", "pqr")
c("abc", "def", "ghi", "jkl") %s+% c("mno", "pqr", "stu", "vwx")
The tidy function for concatenating strings is stringr::str_c(). A sep= can be used to specify a separator for the concatenated strings. |
str_c("hello", "world")
str_c("hello", "world", sep=" ")
We can also concatenate strings using glue::glue(). |
glue("hello", "world")
The traditional base::cat() function returns the concatenation of the supplied strings. Numeric and other complex objects are converted into character strings. |
cat("hello", "world")
cat ("hello", 123, "world")
Yet another alternative (and there are many) is the function base::paste(). Notice that it separates the concatenated strings with a space. |
paste("hello", "world")
|