Create Comma Separated Vector in R
Last Updated :
29 Jul, 2021
In this article, we will discuss how to create a comma-separated vector in R Programming Language.
Method 1 : Using shQuote() method
First, the vector elements are enclosed with single or double quotes based on the input using the shQuote() method. This method can be used to enclose a string in double-quotes. It is used with respect to the OS shells. This function is then applied to each individual element of the vector argument and then returned.
Syntax: shQuote(str, type =)
Parameters :
- str : A character vector to be used
- type : Indicator of the type of shell quoting . "cmd" and "cmd2" are used in the Windows shell. "sh" and "csh" are used in unix-alike shells.
The elements of the vector then obtained are subjected to the paste() method which is used to concatenate the smaller strings into one larger string object separated by the delimiter set by the collapse attribute of this method.
Syntax:
paste (obj, sep = " ", collapse )
Parameters :
- obj - It is a vector or a list or an array object consisting of one or more elements
- sep - The separator used to connect individual elements of obj
- collapse - The elements are combined to a single string, using the connector as collapse value. Default is NULL.
However, we cannot simply use print() method to print the final vector, because backslashes are introduced by the shQuote() method, and they can be eliminated using the cat() method which joins the corresponding elements of the vector.
Example 1:
R
# declaring a string vector
vec <- c('geeks','for','geeks')
print ("Original Vector")
print(vec)
# converting vector
fvec <- shQuote(vec, type = "cmd")
# combining elements using ,
comma_vec <- paste(fvec, collapse = ", ")
# printing the final vector
print ("Final Vector")
cat(comma_vec)
Output
[1] "Original Vector"
[1] "geeks" "for" "geeks"
[1] "Final Vector"
"geeks", "for", "geeks"
Example 2:
R
# declaring a string vector
vec <- letters[5:10]
print ("Original Vector")
print(vec)
# converting vector
fvec <- shQuote(vec, type = "cmd")
# combining elements using ,
comma_vec <- paste(fvec, collapse = ", ")
# printing the final vector
print ("Final Vector")
cat(comma_vec)
print("\n")
# declaring a numerical vector
num_vec <- c(95:100)
print ("Original Vector")
print(num_vec)
# converting vector
vec2 <- shQuote(num_vec, type = "cmd")
# combining elements using ,
comma_vec2 <- paste(vec2, collapse = ", ")
# printing the final vector
print ("Final Vector")
cat(comma_vec2)
Output
[1] "Original Vector"
[1] "e" "f" "g" "h" "i" "j"
[1] "Final Vector"
"e", "f", "g", "h", "i", "j"
[1] "\n"
[1] "Original Vector"
[1] 95 96 97 98 99 100
[1] "Final Vector"
"95", "96", "97", "98", "99", "100"
The paste() method discussed above can also be used solely, in case, we do not wish to necessarily obtain each of the elements of the original vector as an individual string. In this case, the corresponding elements of the vector are just concatenated into a larger string using the string specified in the collapse attribute of the paste() method.
The paste0() method is similar in functionality to paste() method, it just does not allow the user to enter a customized separator string. This method can also be used to generate a comma separated vector.
Syntax:
paste0(…, collapse = NULL)
Example:
R
# declaring a string vector
vec <- c(1:10)
print ("Original Vector")
print (vec)
# using paste method
vec_mod = paste(vec, collapse=",")
print ("Modified Vector using paste")
print (vec_mod)
# using paste0 method
vec_mod1 = paste0(vec, collapse=",")
print ("Modified Vector using paste0")
print (vec_mod1)
Output
[1] "Original Vector"
[1] 1 2 3 4 5 6 7 8 9 10
[1] "Modified Vector using paste"
[1] "1,2,3,4,5,6,7,8,9,10"
[1] "Modified Vector using paste0"
[1] "1,2,3,4,5,6,7,8,9,10"
Similar Reads
Convert Matrix to Vector in R
In this article, we are going to convert the given matrix into the vector in R programming language. Conversion of the matrix to vector by rowMethod 1: Using c() function Simply passing the name of the matrix will do the job. Syntax: c(matrix_name) Where matrix_name is the name of the input matrix E
3 min read
How to Filter a Vector in R
In this article, we are going to discuss how to filter a vector in the R programming language. Filtering a vector means getting the values from the vector by removing the others, we can also say that getting the required elements is known as filtering. Method 1: Using %in% Here we can filter the ele
2 min read
How to create an array from vectors in R ?
In this article, we will discuss how to create an array from the vectors in R Programming Language. We can create an array using array() function. We have to pass the vectors and dim() as parameters. Here dim() function is used to give dimensions for the array. Syntax: array_name = array( c(vector 1
2 min read
Concatenate Vector of Character Strings in R
In this article, we will discuss how to concatenate the strings present in two or more vectors in R Programming Language. Discussed below are various methods of doing so. Method 1: Using paste() paste() function is used to combine strings present in vectors passed to it  an argument. Syntax: paste(
4 min read
Compare two character vectors in R
In this article, we will discuss how to compare two character vectors in R Programming Language. Method 1: Using %in% This operator is used to find the elements present in one vector with respect to the second vector Syntax: vector1 %in% vector2 Return type: It returns boolean values corresponding t
2 min read
Transpose a vector into single column in R
In this article, we will discuss how to convert a vector from single row to a column in R Programming Language. Steps - Create a vector c(value1,value2,.....,valuen) Convert the vector into the matrix. In R, Matrix is a two-dimensional data structure that comprises rows and columns. We can create a
2 min read
How to Create a Vector of Zero Length in R
In this article, we are going to discuss how to create a vector of zero length in R programming language. We can create a vector of zero length by passing the datatype as a value to the vector. Syntax: vector=datatype() There are six data types and each can be used to create a vector of the required
2 min read
R - Create empty vector and append values
In this article, we will discuss how to create an empty vector and add elements into a vector in R Programming Language. An empty vector can be created by simply not passing any value while creating a regular vector using the c() function. Syntax: c() This will return NULL as an output. Example: R #
3 min read
How to convert Excel column to vector in R ?
In this article, we will be looking at the different approaches to convert the Excel columns to vector in R Programming language. The approaches to convert Excel column to vector in the R language are listed as follows: Using $-Operator with the column name.Using the method of Subsetting column.Usin
3 min read
Convert dataframe rows and columns to vector in R
In this article, we are going to convert a dataframe column to a vector and a dataframe row to a vector in the R Programming Language. Convert dataframe columns into vectors We are taking a column in the data frame and passing it into another variable by using the selection method. The selection met
2 min read