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

Unlock this course with a free trial

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

for loop

for loop

- [Instructor] R prefers that you use vector math for repetitive operations on vectors but R also provides a for loop for other repetitive tasks. Let's take a look at for and how it operates. I've created a very simple for loop in lines one, two and three. For bobsYourUncle in on through five print the value of bobsYourUncle. Let's go ahead and run that. So I select line one and hit Run. And you can see in the console that I have the values one, two, three, four, five. It's kind of exactly what you'd expect from a for loop. Now, there's a couple of gotchas here. Let's take a look at one. I'm going to add in a line on line two called bobsYourUncle and I am going to assign the value of three to bobsYourUncle. So you would think that I have now created an endless loop because bobsYourUncle will always be equal to three. Let's run that. Select line one and then run that for loop and what you see is that we have the…

Contents