String Concatenation
Let us start with the simplest of string operations - concatenation two strings.
cat
The cat() function concatenates objects and could also print them to screen or to a file. By default it converts even numeric and other complex objects into character type and then concatenates.
cat("hello", "world", sep="\t")
## hello world
# Using numeric values with cat().
x <- 123
cat ("hello", x, sep="\t")
## hello 123
paste
Alternatively we can use the paste() function to concatenate and print the values to screen.
paste("hello", "world", sep="\t")
## [1] "hello\tworld"
The major disadvantage of the paste function is that it does not handle null characters effectively. It converts the NULL character as a space instead of treating it appropriately. WHAT IS APPROPRIATE?
paste('hello', NULL, 'world')
## [1] "hello world"
str_c
The str_c() function is similar to the paste() function but the default separator is white space and it ignores NULL characters.
str_c('hello', NULL, 'world') # Using str_c with null characters.
## [1] "helloworld"