R brush-up

Author

Boris Hejblum

Published

June 6, 2024

RStudio project setup

  1. Start by opening RStudio and create a new project

Functions

  1. Print function
    Let’s start by writing a function that will print its one argument (e.g. 2).
Code
myprint(2)
[1] 2
  1. Default arguments Let’s now add a default value to this function’s argument

Control structures

  1. for loop
    Now consider the following data.frame:
first_name last_name gender
Hermione Granger female
Ron Weasley male
Harry Potter male

Write a function that takes such a data.frame as a argument and that will print the last name for each row.

  1. while statement
    Now write the same function using a while statement.
  1. if/else statement
    Finally, write a function that will print the last name for females and the first name for males.

There are only two hard things in Computer Science:

  • cache invalidation, and
  • naming things.

– Phil Karlton