Investing in financial products involves risk to your capital.

Asset Classes

Free investment financial education

More Campus Resources

Useful Tools and Information

Language

Multilingual content from IBKR

# RObservations #30: Fixing R’s “Messy String Concatenation” with a Special Function

Benjamin Smith
bensstats

## 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:

``````## 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:

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

``````## 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!

Visit Bensstats Blog for additional insight on this topic and subscribe to his newsletter: https://bensstats.wordpress.com/2022/04/21/robservations-30-fixing-rs-messy-string-concatenation-with-a-special-function/

###### Disclosure: bensstats

All investments carry a certain degree of risk, including the possible loss of principal. There is no assurance that an investment will provide positive performance over any period of time. There are specific risks that apply to investment strategies. These risks should be reviewed carefully before taking any investment action. Since no one investment style or manager is suitable for all types of investors, this commentary is provided for informational purposes only. The statements contained herein are the opinions of Rareview Capital LLC. All opinions and views constitute our judgments as of the date of writing and are subject to change at any time without notice. This commen contains no investment advice or recommendations. Individual investor results will vary. Past performance is no guarantee of future results.

###### Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from bensstats and is being posted with its permission. The views expressed in this material are solely those of the author and/or bensstats and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.