How to repeat groups of letters

I want to create a column with the letters A-D repeating 5 times, but I want to repeat each letter 5 times, not repeat A-D 5 times. For example, I want A A A A A B B B B B etc, NOT A B C D A B C D. This is what I’ve tried, but it doesn’t work.

data$group <- rep(c("A",5), ("B",5), ("C",5), ("D",5))

I get the error:

Error: unexpected ',' in "helpd$Group <- rep(c("A",5), ("B","

So something is wrong with my syntax. Anybody know the appropriate syntax?

Thank you!

>Solution :

you should use each = ... option in rep

> rep(LETTERS[1:4], each = 5)
 [1] "A" "A" "A" "A" "A" "B" "B" "B" "B" "B" "C" "C" "C" "C" "C" "D" "D" "D" "D"
[20] "D"

Leave a Reply