How to replicate strings from a vector differently in R?

Viewed 81

I have this vector:

Photoperiod <- c("Day","Sunset","Night","Sunrise")

I would like to create a vector in which Day is repeated 12 times, Sunset 2 twice, Night 8 times and Sunrise twice until I get a vector of length equal to 168.

How could I do this?

2 Answers

What about:

rep(rep(c("Day","Sunset","Night","Sunrise"), c(12, 2, 8, 2)), length.out = 168)

Another option using one rep and recycling technique.

x <- character(168)
x[] <- rep(Photoperiod, c(12, 2, 8, 2))
Related