简体   繁体   中英

Paste together two character vectors of different lengths

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" 

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" 

outer

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

既然它是两个元素数组,我会这样做

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM