RObservations #30: Fixing R’s “messy string concatenation” with a special function

[This article was first published on r – bensstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Introduction

Recently I discovered stackshare.io’s stackups which offers comparisons of different programming languages as well as their pros and cons. While looking at the all too classic comparison available between R and Python I noticed that one of the cons listed was:

Messy syntax for string concatenation

While it is possible to use the paste() or paste0() for string concatenation. I do understand how it can be messy to deal with, especially when working with loops and/or nested functions. In this short blog I share a remedy for this by writing a special function which can lend for cleaner code as opposed to using paste() or paste0().

The Code

Suppose we are interested in printing out iteratively a list of numbers each appended to a string. Using base R this can either be accomplished with a simple for-loop:

for(i in 1:10){
  cat(paste0("This is the number ", i,"\n"))
}


## This is the number 1
## This is the number 2
## This is the number 3
## This is the number 4
## This is the number 5
## This is the number 6
## This is the number 7
## This is the number 8
## This is the number 9
## This is the number 10

While it is simple enough, the code does already look cramped with the nesting of paste0() within cat(). To have a cleaner set up, as found in python, java and other languages, its possible to write a special function which serves as a wrapper to paste0. The code is:

"%+%" <- function(string1, string2){
  paste0(string1,string2)
}

When used instead of paste0 in the same example, we have a cleaner looking syntax:

for(i in 1:10){
  cat("This is the number " %+% i %+% "\n")
}


## This is the number 1
## This is the number 2
## This is the number 3
## This is the number 4
## This is the number 5
## This is the number 6
## This is the number 7
## This is the number 8
## This is the number 9
## This is the number 10

Conclusion

While it is possible to alter the + operator in R to accommodate string concatenation, writing a script which will handle the edge cases and be a consistent operator is something which I (and anyone else) should be wary of when experimenting with it. Having a special function which basically serves as a wrapper for paste0() allows me to have an operator which is specific to string concatenation and won’t harm the rest of R’s functionality and my trust in the + operator.

This function definitely would be something I would include in a R package, but if you beat me to it and make it before I do, please let me know and be sure to mention this blog!

Want to see more of my content?

Be sure to subscribe and never miss an update!

To leave a comment for the author, please follow the link and comment on their blog: r – bensstats.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)