Home > front end >  Generate a sequence of repeated characters in R
Generate a sequence of repeated characters in R

Time:09-30

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"

CodePudding user response:

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

  • Related