Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
连接r中的两个字符串列表_R_String_Data Manipulation - Fatal编程技术网

连接r中的两个字符串列表

连接r中的两个字符串列表,r,string,data-manipulation,R,String,Data Manipulation,这是我的样本: a = c("a","b","c") b = c("1","2","3") 我需要自动连接a和b。结果应该是“A1”、“A2”、“A3”、“B1”、“B2”、“B3”、“C1”、“C2”、“C3” 目前,我正在使用粘贴功能: paste(a[1],b[1]) 我需要一个自动的方法来做这件事。除了编写循环,还有什么更简单的方法可以实现这一点吗?您可以: c(sapply(a, function(x) {paste(x,b)})) [1] "a 1" "a 2" "a 3" "b

这是我的样本:

a = c("a","b","c")
b = c("1","2","3")
我需要自动连接a和b。结果应该是“A1”、“A2”、“A3”、“B1”、“B2”、“B3”、“C1”、“C2”、“C3”

目前,我正在使用粘贴功能:

paste(a[1],b[1])
我需要一个自动的方法来做这件事。除了编写循环,还有什么更简单的方法可以实现这一点吗?

您可以:

c(sapply(a, function(x) {paste(x,b)}))
[1] "a 1" "a 2" "a 3" "b 1" "b 2" "b 3" "c 1" "c 2" "c 3"
paste0
编辑为
paste
以匹配OP update

其他选项包括:

paste(rep.int(a,length(b)),b)
或:


粘贴有什么问题?它们都应该有空间。我已经编辑了我的问题。外FTW!我总是忘记那个函数:)
with(expand.grid(b,a),paste(Var2,Var1))
c(outer(a, b, paste))

# [1] "a 1" "b 1" "c 1" "a 2" "b 2" "c 2" "a 3" "b 3" "c 3"