如何交替连接3个字符串 [英] how to alternatively concatenate 3 strings

查看:84
本文介绍了如何交替连接3个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个字符串:

a<-c("a1","a2","a3")
b<-c("b1","b2","b3")
c<-c("c1","c2","c3")

如何获得以下输出:

"a1","b1","c1","a2","b2","c2","a3","b3","c3"

这是我尝试的内容:

paste(a,b,c,sep='","')

我得到了什么:

[1] a1\,\ b1\,\ c1 a2\,\ b2\ \,\ c2 a3\,\ b3\,\ c3

是有办法吗?
谢谢。

Is there a way to do it? Thank you.

推荐答案

您还可以使用

c(rbind(a,b,c))

其中( rbind )将三个变量放到一个矩阵中(逐行),然后( c() )利用R按列顺序存储矩阵这一事实,将它们转换回向量。

which (rbind) puts the three variables together into a matrix (row-wise), and then (c()) converts them back into a vector, taking advantage of the fact that R stores matrices in column-wise order.

此问题的某些答案源于(我认为)是因为操作者之间在组合各个字符串(例如 paste( a,)之间缺乏清晰性(不知道这是理解还是沟通的问题……) b)得到长度为1的向量: ab))并组合字符串的 vector c( a, b)得出长度为2的向量: a b

Some of the diversity of the answers to this question stems (I think) from a lack of clarity on the OP's part (don't know whether this is an issue of understanding or communication ...) between combining individual character strings (e.g. paste("a","b") results in a vector of length 1: "a b")) and combining vectors of character strings (c("a","b") results in a vector of length 2: "a" "b")

这篇关于如何交替连接3个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆