1
votes

I'm new to R and while practicing I couldn't work out how to create a vector whose outcome is :

red yellow blue yellow blue green blue green magenta green magenta cyan .

I noticed that

rep(seq(1:3),times=4) + rep(0:3, each=3)

[1] 1 2 3 2 3 4 3 4 5 4 5 6

so I thought it cold be useful to create a vector such as

colors <- c("red", "yellow", "blue","green","magenta","cyan")

and associate its values to the sequence but then I don't know how to go on.

Thank you in advance for any help!

1
If the rep value is the index colors[rep(seq(1:3),times=4) + rep(0:3, each=3)]akrun
@akrun great, thank you so much!Jade

1 Answers

1
votes

We need to use the index vector for expanding the 'colors'

colors[rep(seq(1:3),times=4) + rep(0:3, each=3)]