Add repeating values as new column in R

199 Views Asked by At

I would like to add a new column to my data set that contains the numbers 1 - 46 and repeats them 12 times. That is 12 times 1, 12 times 2, 12 times 3 etc. Can anyone help me how to do this?

Thank you!

I tried

new <- rep(c(1:46, each,  times = 12))

but it gives the numbers 1 - 46 ongoing..

1

There are 1 best solutions below

0
SteveM On

Note that R will use vectors multiple times as long as the vector length is an integer multiple of the reference vector, e.g.

df <- data.frame(a = 1:92, b = 1:46)

generates a dataframe with column a = 1:92 and column b = 1:46 twice. The reference vector is 1:nrow(df). In your case 1:46 declared once would be replicated 11 times