Skip to content
Related Articles

Related Articles

Improve Article

Create Comma Separated Vector in R

  • Last Updated : 17 May, 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"

Method 2 : Using paste() or paste0() method

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 ("Modifed Vector using paste")
print (vec_mod)
  
# using paste0 method
vec_mod1 = paste0(vec, collapse=",")
print ("Modifed Vector using paste0")
print (vec_mod1)

Output

[1] "Original Vector"
[1]  1  2  3  4  5  6  7  8  9 10
[1] "Modifed Vector using paste"
[1] "1,2,3,4,5,6,7,8,9,10"
[1] "Modifed Vector using paste0"
[1] "1,2,3,4,5,6,7,8,9,10"



My Personal Notes arrow_drop_up
Recommended Articles
Page :