Tutorial by Examples

In this example we will calculate the squared deviance for each column in a data frame, in this case the mtcars. Option A: integer index squared_deviance <- vector("list", length(mtcars)) for (i in seq_along(mtcars)){ squared_deviance[[i]] <- (mtcars[[i]] - mean(mtcars[[i]]))^2...
To illustrate the effect of good for loop construction, we will calculate the mean of each column in four different ways: Using a poorly optimized for loop Using a well optimized for for loop Using an *apply family of functions Using the colMeans function Each of these options will be shown...
R provides two additional looping constructs, while and repeat, which are typically used in situations where the number of iterations required is indeterminate. The while loop The general form of a while loop is as follows, while (condition) { ## do something ## in loop body } whe...

Page 1 of 1