Create Comma Separated Vector in R (Example)

 

In this R article you’ll learn how to create a vector separated by commas.

Table of contents:

Let’s start right away.

 

Example Data

As a first step, we’ll have to define some data that we can use in the following examples:

x <- c(4, 1, 4, 2, 3, 5)                   # Example vector
x                                          # Print vector
# 4 1 4 2 3 5

Have a look at the previous output of the RStudio console. It shows that the example data is an array containing six numeric values.

 

Example: Convert Numeric Vector to Comma Separated

The following R programming syntax shows how to use the paste & shQuote functions to change our numeric example vector to a comma separated character vector.

x_cs <- paste(shQuote(x, type = "cmd"),    # Converting vector
              collapse = ", ")
cat(x_cs)                                  # Print updated vector
# "4", "1", "4", "2", "3", "5"

As you can see based on the output of the RStudio console, our new vector is a comma separated vector.

 

Video, Further Resources & Summary

Have a look at the following video of my YouTube channel. I show the topics of this tutorial in the video.

 

The YouTube video will be added soon.

 

Furthermore, you may have a look at the other tutorials which I have published on this homepage. Some tutorials are shown below.

 

At this point you should have learned how to separate vectors or arrays by comma in the R programming language. If you have further questions, please let me know in the comments.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top