Paste together two character vectors of different lengths

只愿长相守 提交于 2020-01-21 03:20:07

问题


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" 

回答1:


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" 



回答2:


outer

outer(groups, labels, FUN=paste)




回答3:


Since it's two element array, I would do

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


来源:https://stackoverflow.com/questions/7287406/paste-together-two-character-vectors-of-different-lengths

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!