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.
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.
- Convert Character to Factor in R
- type.convert R Function
- Concatenate Vector of Character Strings
- Convert a Character to Numeric
- Convert a Factor to Numeric
- The R Programming Language
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.
Statistics Globe Newsletter