MAPK MAPK - 3 months ago 16
R Question

Expand vector with different extensions

I have a vector called

myvec
( with more than 10000 items). I want to expand the vector with the extensions (
.GT
,
.AD
,
.DP
) in the corresponding order and get the
result
.

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


result:

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

Answer

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)))

data

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