4.3 Vectorisation
The brilliance of using R for data analysis is the concept of vectorisation. Because we generally work with a table of values and want to perform the same operation again either on the whole table, a whole row or a whole column. Vectorisation means a function can be applied to every element of a vector or matrix without having to explicitly loop through each element of the whole variable.
For example, let’s say we want to convert our temperatures from Celsius to Kelvin, we can do this simply by one line of code:
head(minTemp$X1996 + 273.15)
## [1] 296.55 296.65 297.85 290.95 293.55 293.65
Performing a vectorised function on a vector or matrix does not change the contents of that variable. To perform an in-place calculation, you need to explicitly overwrite the variable with the output of the function:
kelvinTemp <- minTemp + 273.15