MAPK MAPK - 1 year ago 92
R Question

Expand vector with different extensions

I have a vector called

( with more than 10000 items). I want to expand the vector with the extensions (
) in the corresponding order and get the

myvec<- c("Apple","Ball","Car")


Apple.GT, Apple.AD, Apple.DP, Ball.GT, Ball.AD, Ball.DP, Car.GT, Car.AD, Car.DP

Answer Source

We can use outer

c(outer(myvec, v1, FUN  = paste0))

If we need to change the order

c(t(outer(myvec, v1, FUN  = paste0)))
#[1] "Apple.GT" "Apple.AD" "Apple.DP" "Ball.GT"  "Ball.AD"  "Ball.DP"  
#[7] "Car.GT"   "Car.AD"   "Car.DP"  

Or a faster option would be rep with paste

paste0(rep(myvec, length(v1)), rep(v1, each = length(myvec)))


v1 <-c(".GT",".AD",".DP")