Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expand vector with different extensions

Tags:

r

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
like image 241
MAPK Avatar asked Sep 09 '26 15:09

MAPK


1 Answers

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")
like image 160
akrun Avatar answered Sep 12 '26 18:09

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!