字符的串连矢量中的R
在本文中,我们将讨论如何在 R 编程语言中连接出现在两个或多个向量中的字符串。
下面讨论了这样做的各种方法。
方法 1:使用 paste()
paste()函数用于组合传递给它的向量中存在的字符串作为参数。
Syntax: paste(vector1,vector2,.,vector n,sep=”symbol”)
Parameter:
- vectors are the input vectors to be concatenate
- sep is the separator symbol that separates the strings present in the vector.
示例 1:
R
# create a vector with character
# strings(names)
a=c("manoj","sravan","harsha")
# create a vector with character
# strings (address)
b=c("vijayawada","ponnur","hyd")
# concatenate these two vectors
# using paste function
print(paste(a,b,sep="--"))
R
# create a vector with character
# strings(names)
a=c("manoj","sravan","harsha")
# create a vector with character
# strings (address)
b=c("vijayawada","ponnur","hyd")
# create a vector with character
# strings(subjects)
d=c("java",".net","python")
# create a vector with character
# strings (college)
e=c("iit","srm-ap","vignan")
# create a vector with character
# strings(department)
f=c("cse","food tech","ece")
# concatenate these five vectors
# using paste function
print(paste(a,b,d,e,f,sep="--"))
R
# create a vector with character
# strings(names)
a=c("manoj","sravan","harsha")
# create a vector with character
# strings (address)
b=c("vijayawada","ponnur","hyd")
# create a vector with character
# strings(subjects)
d=c("java",".net","python")
# create a vector with character
# strings (college)
e=c("iit","srm-ap","vignan")
# create a vector with character
# strings(department)
f=c("cse","food tech","ece")
# concatenate these five vectors
# using paste function
print(paste(a,b,d,e,f))
R
# create a vector with character
# strings(names)
a=c("manoj","sravan","harsha")
# create a vector with character
# strings (address)
b=c("vijayawada","ponnur","hyd")
# create a vector with character
# strings(subjects)
d=c("java",".net","python")
# create a vector with character
# strings (college)
e=c("iit","srm-ap","vignan")
# create a vector with character
# strings(department)
f=c("cse","food tech","ece")
# concatenate these five vectors
# using cbind function
print(cbind(a,b,d,e,f))
R
# create a vector with character
# strings(names)
a=c("manoj","sravan","harsha")
# create a vector with character
# strings (address)
b=c("vijayawada","ponnur","hyd")
# create a vector with character
# strings(subjects)
d=c("java",".net","python")
# create a vector with character
# strings (college)
e=c("iit","srm-ap","vignan")
# create a vector with character
# strings(department)
f=c("cse","food tech","ece")
# concatenate these five vectors
# using rbind function
print(rbind(a,b,d,e,f))
R
# create a vector with character
# strings(names)
a=c("manoj","sravan","harsha")
# create a vector with character
# strings (address)
b=c("vijayawada","ponnur","hyd")
# concatenate these two vectors
# using cat function
print(cat(a,b))
R
# create a vector with character
# strings(names)
a=c("manoj","sravan","harsha")
# create a vector with character
# strings (address)
b=c("vijayawada","ponnur","hyd")
# create a vector with numeric data
d=c(1,2,3,4,5)
# create a vector with numeric data
e=c(1.6,2.2,3.78,4.4456,5.4)
# concatenate these four vectors using
# cat function
print(cat(a,b,d,e))
输出:
[1] “manoj–vijayawada” “sravan–ponnur” “harsha–hyd”
示例 2:
电阻
# create a vector with character
# strings(names)
a=c("manoj","sravan","harsha")
# create a vector with character
# strings (address)
b=c("vijayawada","ponnur","hyd")
# create a vector with character
# strings(subjects)
d=c("java",".net","python")
# create a vector with character
# strings (college)
e=c("iit","srm-ap","vignan")
# create a vector with character
# strings(department)
f=c("cse","food tech","ece")
# concatenate these five vectors
# using paste function
print(paste(a,b,d,e,f,sep="--"))
输出:
[1] "manoj--vijayawada--java--iit--cse"
[2] "sravan--ponnur--.net--srm-ap--food tech"
[3] "harsha--hyd--python--vignan--ece"
如果没有分隔符,向量将在没有任何空格或任何符号的情况下组合。
示例 3:
电阻
# create a vector with character
# strings(names)
a=c("manoj","sravan","harsha")
# create a vector with character
# strings (address)
b=c("vijayawada","ponnur","hyd")
# create a vector with character
# strings(subjects)
d=c("java",".net","python")
# create a vector with character
# strings (college)
e=c("iit","srm-ap","vignan")
# create a vector with character
# strings(department)
f=c("cse","food tech","ece")
# concatenate these five vectors
# using paste function
print(paste(a,b,d,e,f))
输出:
[1] “manoj vijayawada java iit cse” “sravan ponnur .net srm-ap food tech”
[3] “harsha hyd python vignan ece”
方法 2:使用 cbind()
cbind()函数用于按列组合向量,即它将第一个向量放在第一列中,将第二个向量放在第 2 列中,依此类推。
Syntax: cbind(x1, x2, …, deparse.level = 1)
Parameters:
x1, x2: vector, matrix, data frames
deparse.level: This value determines how the column names generated. The default value of deparse.level is 1.
例子:
电阻
# create a vector with character
# strings(names)
a=c("manoj","sravan","harsha")
# create a vector with character
# strings (address)
b=c("vijayawada","ponnur","hyd")
# create a vector with character
# strings(subjects)
d=c("java",".net","python")
# create a vector with character
# strings (college)
e=c("iit","srm-ap","vignan")
# create a vector with character
# strings(department)
f=c("cse","food tech","ece")
# concatenate these five vectors
# using cbind function
print(cbind(a,b,d,e,f))
输出:
a b d e f
[1,] “manoj” “vijayawada” “java” “iit” “cse”
[2,] “sravan” “ponnur” “.net” “srm-ap” “food tech”
[3,] “harsha” “hyd” “python” “vignan” “ece”
方法 3:使用rbind()
rbind() 按行连接向量字符串,即第 1 行是向量 1,第 2 行是向量 2,依此类推。
Syntax: rbind(x1, x2, …, deparse.level = 1)
Parameters:
x1, x2: vector, matrix, data frames
deparse.level: This value determines how the column names generated. The default value of deparse.level is 1.
例子:
电阻
# create a vector with character
# strings(names)
a=c("manoj","sravan","harsha")
# create a vector with character
# strings (address)
b=c("vijayawada","ponnur","hyd")
# create a vector with character
# strings(subjects)
d=c("java",".net","python")
# create a vector with character
# strings (college)
e=c("iit","srm-ap","vignan")
# create a vector with character
# strings(department)
f=c("cse","food tech","ece")
# concatenate these five vectors
# using rbind function
print(rbind(a,b,d,e,f))
输出:
[,1] [,2] [,3]
a "manoj" "sravan" "harsha"
b "vijayawada" "ponnur" "hyd"
d "java" ".net" "python"
e "iit" "srm-ap" "vignan"
f "cse" "food tech" "ece"
方法四:使用 cat()
cat()函数用于连接给定的向量。
句法:
cat(vector1,vector2,.,vector n)
其中,向量是输入向量
它产生一个一维向量,最后一个值为 NULL
示例 1:
电阻
# create a vector with character
# strings(names)
a=c("manoj","sravan","harsha")
# create a vector with character
# strings (address)
b=c("vijayawada","ponnur","hyd")
# concatenate these two vectors
# using cat function
print(cat(a,b))
输出:
manoj sravan harsha vijayawada ponnur hydNULL
示例 2:
电阻
# create a vector with character
# strings(names)
a=c("manoj","sravan","harsha")
# create a vector with character
# strings (address)
b=c("vijayawada","ponnur","hyd")
# create a vector with numeric data
d=c(1,2,3,4,5)
# create a vector with numeric data
e=c(1.6,2.2,3.78,4.4456,5.4)
# concatenate these four vectors using
# cat function
print(cat(a,b,d,e))
输出:
manoj sravan harsha vijayawada ponnur hyd 1 2 3 4 5 1.6 2.2 3.78 4.4456 5.4NULL