Site icon R-bloggers

Difference Between cat() and paste() in R

[This article was first published on Data Analysis in R, 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.

The post Difference Between cat() and paste() in R appeared first on finnstats.

If you are interested to learn more about data science, you can find more articles here finnstats.

Difference Between cat() and paste() in R, you can concatenate strings together using both the cat() and paste() functions, however, they differ differently in the following ways:

The concatenated text will be displayed to the console by the cat() function, but the results won’t be saved in a variable.

The concatenated string will be written to the console using the paste() function, and the results will be saved in a character variable.

The cat() function is typically more frequently used for troubleshooting.

The paste() function, on the other hand, is used when you want to save the concatenation’s results in a character variable and later refer to that variable in your code.

Difference Between cat() and paste() in R

The examples that follow demonstrate practical applications for each function.

Example: How to Use the cat() Function

The code below demonstrates how to combine multiple strings using the cat() function.

join a number of strings together.

cat("finnstats", "for", "datascience")
finnstats for datascience

The cat() function joins the three strings together to create a single string and shows the results to the console, as you can see.

However, if we try to put the concatenation’s results in a variable and then access that variable, we’ll get a NULL value.

Now we can concatenate several strings together

results <- cat("finnstats", "for", "datascience")
finnstats for datascience

Now let’s try to view the concatenated string

results
NULL

This is due to the cat() function’s lack of result storage.

Just the results are output to the console.

Example: How to Use the paste() Function

The code below demonstrates how to combine multiple strings using the paste() function:

join a number of strings together.

paste("finnstats", "for", "datascience")
[1] "finnstats for datascience"

The paste() function joins the three strings together to create a single string and reports the results to the console, as can be seen.

The concatenated string can be viewed by referring to the variable in which the concatenation results were saved.

join a number of strings together.

results <- paste("finnstats", "for", "datascience")

Let’s view the concatenated string

results
[1] "finnstats for datascience"

Because the paste() function saves the output in a character variable, we can inspect the concatenated string.

In order to view the length of the concatenated text, we may use utilize functions like nchar():

Show how many characters are in the concatenated string.

nchar(results)
[1] 25

The concatenated string has 25 characters, as can be seen (including spaces).

Since cat() doesn’t put the results in a variable, we couldn’t utilize the nchar() function with it.

Find out which data skills are most in demand? »

Check your inbox or spam folder to confirm your subscription.

If you are interested to learn more about data science, you can find more articles here finnstats.

The post Difference Between cat() and paste() in R appeared first on finnstats.

To leave a comment for the author, please follow the link and comment on their blog: Data Analysis in R.

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.