Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading