Insert two character vectors of different lengths together

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 to use 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 parameter that can insert them 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" 
+6
source share
3 answers

Perhaps outer helps your work. Try the following:

 > 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" 
+15
source

outer

external (groups, tags, FUN = insert)

+4
source

Since these are two elements of an array, I would do

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

Source: https://habr.com/ru/post/896514/


All Articles