How to do it...

Making use of the apply family of functions can be done using the following steps:

  1. Create a matrix and use apply to work on it:
m <- matrix(rep(1:10, 10, replace = TRUE), nrow = 10)

apply(m, 1, sum)
apply(m, 2, sum)

  1. Use lapply over the vector:
numbers <- 1:3
number_of_numbers <- function(x){
rnorm(x)
}
my_list <- lapply(numbers, number_of_numbers)
  1. Use lapply and sapply over the list:
summary_function <- function(x){
mean(x)
}
lapply(my_list, summary_function)
sapply(my_list, summary_function)
  1. Use lapply over a dataframe:
list_from_data_frame <- lapply(iris, mean, trim = 0.1, na.rm = TRUE )
unlist(list_from_data_frame)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.49.190