Paste0 VS. Assignment Operator

Hey everyone, I recently came across the cat, paste, and paste0 functions in R.
I understand that the cat function can't be stored as a variable, so it's probably not that useful. The paste0 function can separate strings by special characters or spaces if you want them too, so it's better than the paste function, if that's your purpose.

However, why would someone not just simply use an assignment operator? You don't need to use any specific function that may restrict you from anything, and you can type out whatever you want. Is it more of a preference thing since sometimes we may want to remember to be restricted from doing certain things? Thank you.

a <- paste("Today", "is", "sunny)

b <- paste0(c("Today", "is","sunny"), collapse = "-")

c <- "Today is sunny"

c is so much simpler. Why would someone choose the other functions? Hopefully my question makes sense.

1 Like

Simple example for repetition. Assignment woult not be very gelpful.

XX <- 1:9
paste0("Q.", XX)
2 Likes
a <- "first string"
b <- "second string"

c <- paste(a,b) # tough to do this one with assignment operator.
2 Likes

Thank you for the reply! I'm noticing that this is different from the rep() function. Because with rep("Q.", 9) it would print "Q." 9 times. But with the paste0 function it doesn't paste "Q." 9 times, it pastes "Q.1", "Q.2", etc., up until Q.9. Would there be a way for me to paste "Q." 9 individual times like with rep() ? Or is that just the rules with the paste0 function?

Do you mean like this?

paste0("Q.", rep(9,  20))

[1] "Q.9" "Q.9" "Q.9" "Q.9" "Q.9" "Q.9" "Q.9" "Q.9" "Q.9" "Q.9" "Q.9" "Q.9" "Q.9" "Q.9" "Q.9" "Q.9" "Q.9" "Q.9" "Q.9" "Q.9"

Though for this example there isn't really any point in using rep().

2 Likes

Yes! Thank you for showing this example!

1 Like

A couple small additional notes:

paste0() is the same as paste(sep = ""), it's just a convenience because it's common to use an empty separator. Otherwise paste() can accept any separator, including special characters.

cat(), print(), or message() are meant to write in the console; they can assemble strings as a convenience, but that's not the main goal. On the other hand, paste() is meant to assemble a string, without printing it in the console.

If you wanted, you could always pre-assemble a string with paste() and print it with cat(), for example:

a <- "my"
b <- "string"
assembled <- paste(a, b)
cat(assembled)

but it's faster to use cat("my", "string", sep = " ").

However, cat() or similar is necessary if you want to force immediate printing, for example:

my_function <- function(){
  cat("This is printed immediately\n")
  
  Sys.sleep(1)
  
  cat("This is printed later\n")
  
  stop("error")
  
  cat("This is never printed\n")
  
  return(12)
}


my_function()
#> This is printed immediately
#> This is printed later
#> Error in my_function(): error

That can be useful to write things in the console no matter what happens next, it has a different goal from paste().

1 Like