课程: R for Data Science: Lunch Break Lessons

免费学习该课程!

今天就开通帐号,24,100 门业界名师课程任您挑!

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…

内容