From the course: R for Data Science: Lunch Break Lessons

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Use rep() to create long repetitive vectors

Use rep() to create long repetitive vectors

From the course: R for Data Science: Lunch Break Lessons

Use rep() to create long repetitive vectors

- To create long repetitive vectors you can use "rep". Let's take a look at it. Very simply, rep repeats things. So, rep, parentheses, let's repeat "hello" and we'll repeat it, oh, five times. There it is, five hellos. That's rep. Very, very simple. Ah! But wait! There's more. We can change how rep works by changing that final parameter, let's say I want 10 of them. So I can go length.out equals 10. Now this would be useful if you assigned length.out to the length of a particular variable. And that will tell you how many to send out instead of counting one, two, three, four, five. Rep can also repeat vector elements. So, rep, and then let's create a vector. And in that vector we'll place quote hello and the element bob. So I've got a vector now with two elements. And I'd like to repeat that three times. What I got is hello and then bob, and then hello and then bob, and then hello and bob.…

Contents