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

时间:2011-09-02 17:55:53

标签: r paste

我在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" 

3 个答案:

答案 0 :(得分:17)

可能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" 

答案 1 :(得分:4)

outer

外(组,标签,FUN =粘贴)

答案 2 :(得分:1)

因为它是两个元素数组,所以我会做

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