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 Column names


1.1 rename()

The rename(new_name = old_name) function in dplyr package is designed to change the name of a dataframe

1.2 clean_names()

The clean_names() function in “janitor” package modifies column names consisit of number, letters, and "_".

Exercise 1

import data from the “ozone_samples_demo.csv” file and clean the column names

2 Control structures


Control structures in R allow you to control the flow of execution of a series of R expressions.

The if and else combination is probably the most commonly used control structure in R. It is a great tool to test a condition and take actions depending on whether the condition is TRUE or FALSE.


2.2 ifelse()

The ifelse() funtion is a shorthand form of the if-else statement. It can take a logical vector for testing.

ifelse(test, do something when test is TRUE, do something when test is FALSE)

Exercise 2

x <- runif(10, -20, 20)

with the given vector x, write R code to tell if each element is negative or positive.

Treat zero as positive.

ifelse() and mutate() make a powerful combination in creating new columns.

Exercise 3

Calculate and compare mean ozone concentration between weekdays and weekends using the data in exercise 1

2.3 case_when()

The case_when() function allows you to work on multiple tests. It also takes the logical vector for testing.

case_when(test1 ~ do something when test1 is true, test2 ~ do something different when test2 is true)

Exercise 4

Calculate and compare mean ozone concentration among seasons using the data in exercise 1


3 Recap



  • column names

  • control structures: if - else, ifelse(), case_when()