Vectorised operation versus for loop
One of the well-known drawbacks in R is use of the for
loop. This is relatively very slow compared to other software. However, if you have avoided the use of loop while doing the same task using the matrix operation, then it is a lot faster. In this recipe, you will compare a vectorized version with a for
loop to do the same task.
Getting ready
Consider a situation where you have a dataset with 500 columns representing 500 binary variables of a certain characteristic. In the dataset, you have a total of 900 observations. The objective is to calculate the frequency of 1's in each of the 500 variables and all the possible pair-wise combinations of the variables. The dataset is given in the CSV file, so to implement this recipe, you will need to read the dataset from the CSV file and then convert it into a matrix as follows:
bigBinaryData <- read.csv("loopVectorization.csv") binMat <- as.matrix(bigBinaryData) # to convert data frame into a ...