Operations

Learn how operators, conditions, loops and functions work together.

Required preparation

Work through the following parts of the accompanying Base R course in order. Focus on understanding a small example for each operation before starting Assignment 02.

Topic What you need to be able to do
Operators Calculate values and compare them; distinguish assignment from comparison.
If-then-else Choose an action using a logical condition.
For loops Repeat an operation over a sequence and store the results.
Functions Define an input, call a function and explain the value it returns.
The apply family and lapply: basic structure Pass a function to lapply() and keep its returned list.

A function returns the value of its last expression, or a value supplied with return(). lapply() collects one returned result for each input element in a list. Assign that list to an object to keep it. unlist() flattens a list into a vector; combining different types can convert the values to a common type.

You should be able to explain the difference between printing a result and returning it. These ideas are reused when you write a cleaning function in Unit 04. The later examples on while loops and more elaborate uses of lapply() are additional reading.

Additional explanations

See the Base R module overview, the Quick-R operators page or the DataCamp video.

Updated: