Understanding apply() Family Functions and Vector Data Structures

Objective: We will explore and practice using the apply() family of functions in R to operate on vectors and understand different data structures.

Problem Statement:

Explore apply() family functions and find brief explanation of the differences between tapply(), sapply(), lapply(), and apply().

Perform the following tasks:

  1. Create a numeric vector x containing values (1, 2, 3, 4, 5).

  2. Create a matrix mat with dimensions 3x3 containing values from 1 to 9.

  3. Using suitable apply() family function, calculate the mean of vector x.

  4. Using apply(), calculate the sum of each column in matrix mat.

  5. Using lapply(), convert each element of vector x to its squared value.

  6. Using sapply(), convert each column of matrix mat to a vector and calculate the median of each column.

  7. Using vapply(), verify the type of each element in vector x as numeric.

  8. Print the results of each operation to verify correctness and understanding.

Hints: