In this section, we will compare the code side by side to solve the same problem using both default R
and plyr
. Reusing the iris3
data, we are now interested in producing five-number summary statistics for each variable group by species. The five numbers will be minimum, mean, median, maximum, and standard deviation. The output will be a list of data frames.
To calculate the five-number summary statistics, follow these steps:
Define a function that will calculate five-number summary statistics for a given vector.
Produce the output of this function in a data frame object.
Apply this function in the
iris3
dataset using afor
loop.Apply the same function using the
apply()
function of theplyr
package.
An example that explains the calculation of the five-number summary statistics is as follows:
# Function to calculate five number summary fivenum.summary <- function(x) { results <-data.frame(min=apply(x,2,min), mean=apply(x,2,mean), median=apply(x,2,median), max...