Shortcuts to making your R code run faster! Part 1
The goal of running code is usually to make tasks easier, faster, and more efficient. Sometimes however, it may feel like you are always waiting for your code to run. We will go through a few fun tips that can help speed up that process. 1. Preset your vector or Matrix destinations. Believe it or not that presetting your vector destinations saves your code a lot of running time. This is because when you run a loop or a function and you don't preset your destination R has to hold those variables in its cache memory, however, if you have preset vector ready for variables the values will be routed to that vector. Take this simple example below for instance: # Creating a destination with non-specific number of variables for output vector X = c () # Start timing the loop Strt <- Sys.time () for (i in 1 : 100000 ){ X[i] = i } print ( Sys.time () - Strt) # Print how long it takes to run the loop ## Time difference of 8.179752 secs Ok lets try it with a m...