load the iris dataset

data(iris)

Check out the data and make sure we understand its structure.

head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

We want to calculate a value for every row so lets create a vector for those results.

result <- vector()

Now we are ready to use a control structure and iterate through a dataset.

for(i in 1:nrow(iris)){
  result[i] <- iris$Petal.Width[i]/iris$Petal.Length[i]
}

lets plot the results:

plot(result)

That is interesting looks like there might be unique different groups. If we look at the last column we see that there are actually 3 species. The species are in grouped species 1 is on rows 1-50 two is 51-100 and the last one is on rows 101-150. Lets draw some lines to separate the species and see if that makes sense.

plot(result)
abline(v=50, col="red")
abline(v=100, col="red")