展开不同扩展名的矢量

时间:2016-08-03 06:10:48

标签: r

我有一个名为myvec的矢量(超过10000个项目)。我想按照相应的顺序展开带有扩展名(.GT.AD.DP)的向量,并获取result

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

结果:

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

1 个答案:

答案 0 :(得分:4)

我们可以使用outer

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

如果我们需要更改订单

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"  

或者更快的选项是rep paste

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

数据

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