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


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: Interactive Brokers

Information posted on IBKR Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight 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 permission from bensstats. The views expressed in this material are solely those of the author and/or bensstats and IBKR 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 sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. 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.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.



Source link

Leave a Reply

Your email address will not be published.

Previous Article

Flexjet first to install Ka-Band internet in Legacy 500

Next Article

Value Strikes Back

Related Posts
Read More

Section tags in Layout

armin September 14, 2022, 6:48pm #1 Heyo: i had my sketchup scenes set to show section planes in…