将两个不同长度的字符向量粘贴在一起

发布于 2024-12-03 02:55:54 字数 476 浏览 1 评论 0原文

我在 R 中有两个不同的字符向量,我想将它们组合起来用于列名称:

groups <- c("Group A", "Group B")
label <- c("Time","Min","Mean","Max")

当我尝试使用粘贴时,我得到结果:

> paste(groups,label)
[1] "Group A Time" "Group B Min"  "Group A Mean" "Group B Max"

是否有一个简单的函数或设置可以将它们粘贴在一起以获得以下输出?

[1] "Group A Time" "Group A Min"  "Group A Mean" "Group A Max"  "Group B Time"
[6] "Group B Min"  "Group B Mean" "Group B Max" 

I have two different character vectors in R, that I want to combine to use for column names:

groups <- c("Group A", "Group B")
label <- c("Time","Min","Mean","Max")

When I try using paste I get the result:

> paste(groups,label)
[1] "Group A Time" "Group B Min"  "Group A Mean" "Group B Max"

Is there a simple function or setting that can paste these together to get the following output?

[1] "Group A Time" "Group A Min"  "Group A Mean" "Group A Max"  "Group B Time"
[6] "Group B Min"  "Group B Mean" "Group B Max" 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戏舞 2024-12-10 02:55:54

也许 outer 对您的工作有帮助。试试这个:

> c(t(outer(groups, label, paste)))
[1] "Group A Time" "Group A Min"  "Group A Mean" "Group A Max"  "Group B Time" "Group B Min" 
[7] "Group B Mean" "Group B Max" 

Probably outer helps your work. Try this:

> c(t(outer(groups, label, paste)))
[1] "Group A Time" "Group A Min"  "Group A Mean" "Group A Max"  "Group B Time" "Group B Min" 
[7] "Group B Mean" "Group B Max" 
爱要勇敢去追 2024-12-10 02:55:54

outer

外部(组、标签、FUN=粘贴)

outer

outer(groups, labels, FUN=paste)

信仰 2024-12-10 02:55:54

由于它是两个元素数组,我会这样做

 c(paste(groups[1],label),paste(groups[2],label))

Since it's two element array, I would do

 c(paste(groups[1],label),paste(groups[2],label))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文