how to create a vector of different repeated strings in R

735 Views Asked by At

I have a vector called samples which is the following:

my_samples = c("16S-P030N" ,"16S-P034N", "16S-P035N")

I need to create a vector of each value repeated 30 times so what I usually do is the following:

rep_samples_vector = c(rep("16S-P030N",30),rep("16S-P034N",30), rep("16S-P035N",30))

The problem is that I have near 50 samples, so is there a way to do this in a faster way in R ? I was thinking on using a for loop but I don't have clea1r how to do that in R?

1

There are 1 best solutions below

0
M.Viking On

The rep() function has an each= attribute that will do what you want:

my_samples = c("16S-P030N" ,"16S-P034N", "16S-P035N")
rep(my_samples,  each=30)