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"
The concatenation of vectors can be done by using combination function c. For example, if we have three vectors x, y, z then the concatenation of these vectors can be done as c(x,y,z). Also, we can concatenate different types of vectors at the same time using the same same function.
You can use the paste() and paste0() functions in R to concatenate elements of a vector into a single string. The paste() function concatenates strings using a space as the default separator. The paste0() function concatenates strings using no space as the default separator.
Character/string – each element in the vector is a string of one or more characters. Built in character vectors are letters and LETTERS which provide the 26 lower (and upper) case letters, respecitively. > y = c("a", "bc", "def")
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
outer(groups, labels, FUN=paste)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With