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

Generate a sequence of repeated characters in R

Let’s say that I have three letters: a,b,c .
How can I get the following sequence:

"a" "a" "a" "b" "b" "b" "c" "c" "c"

>Solution :

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

There are different ways to use the rep function to make sequences,
one way is with the each to repeat each element in a vector a number of times, and with the times argument, that let you repeat the whole vector a number of times.

Check both in usage here

rep(c('a', 'b', 'c'), each = 3)
#> [1] "a" "a" "a" "b" "b" "b" "c" "c" "c"
rep(c('a', 'b', 'c'), times = 3)
#> [1] "a" "b" "c" "a" "b" "c" "a" "b" "c"

Created on 2022-09-29 with reprex v2.0.2

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