在 R 中创建逗号分隔的向量
在本文中,我们将讨论如何在 R 编程语言中创建逗号分隔的向量。
方法一:使用 shQuote() 方法
首先,根据使用 shQuote() 方法的输入,向量元素用单引号或双引号括起来。此方法可用于将字符串括在双引号中。它用于 OS shell。然后将此函数应用于向量参数的每个单独元素,然后返回。
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.
然后获得的向量元素经过paste()方法,该方法用于将较小的字符串连接成一个较大的字符串对象,该对象由此方法的collapse 属性设置的定界符分隔。
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.
但是,我们不能简单地使用 print() 方法来打印最终向量,因为反斜杠是由 shQuote() 方法引入的,并且可以使用 cat() 方法将向量的相应元素连接起来来消除它们。
示例 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)
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)
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)
输出
[1] "Original Vector"
[1] "geeks" "for" "geeks"
[1] "Final Vector"
"geeks", "for", "geeks"
示例 2:
电阻
# 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)
输出
[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"
方法 2:使用paste()或paste0()方法
上面讨论的 paste() 方法也可以单独使用,以防万一,我们不希望将原始向量的每个元素都作为单独的字符串。在这种情况下,向量的相应元素只是使用 paste() 方法的collapse 属性中指定的字符串连接成一个更大的字符串。
paste0() 方法在功能上类似于 paste() 方法,它只是不允许用户输入自定义的分隔符字符串。此方法还可用于生成逗号分隔的向量。
句法:
paste0(…, collapse = NULL)
例子:
电阻
# 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)
输出
[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"