A New Week!



We aren’t computer scientists and that’s okay!

We make lots of mistakes. Mistakes are funny. You can laugh with us.

Let’s go, Simba, Pumbaa, and Timon!





1 list - another data structure


List is a special type of vector. It is not limited to single data type. A list can contain any mixture of data types even mixtures of data structures.

We use the list() to create a list.

We can also create an empty list with predefined length with the vector() function

Vectors can be converted to lists using the as.list() function

2 apply family of functions


R has some functions which implement looping in a compact form to make your code easier to write and to read.

lapply(): takes a vector or list and applys a function on each item element.

sapply(): Same as lapply but try to simplify the result

apply(): takes a data frame and applys a function on each row or each column of the input.


2.1 lapply()

This function does the following series of operations:

  1. it loops over a list, iterating over each element

  2. it applies a function to each element of the list

  3. returns a list

Here we are passing mean() as an argument to lapply(). When we pass a function to another function, we do not need to include () like we call a function.

Any arguments in the … argument will get passed down to the function that applied to the list.

Sometime, you will see lappy() and its friends use anonymous functions. These are functions are generated inside lapply(). Once the call to lapply() is finished, the function disappears.

If the function is going to be used a lot in other parts of your code, you might want to define it separately. But if it’s going to be used just for this call to lapply(), then it’s probably simpler to use an anonymous function.

Exercise

Use lapply() to seperate and save airquality data by month.

2.2 sapply()

The sapply() function behaves similarly to lapply(); the only real difference is in the return value.


2.3 apply()

This function takes a matrix or data frame and performs a function on each row or each column of the input individually (1 for rows, 2 for columns).

Usually there are better alternatives for column-wise operations on data frames.


3 Recap



  • list

  • apply family of functions