# Lesson 1: R basics

## Course introduction: mixed models in R

• Follow along with lesson slides and less on text
• Each lesson has a worksheet
• Fill in the ... in the worksheet with code
• Typing in the code yourself is better than copy+paste!
• Optional exercises at the end
• This is a practical skills course, not “Principles of Statistics 101!”

## Day 1 Schedule

Time Activity
9:00-9:15 AM Introductions, troubleshooting
9:15-10:15 AM Lesson 1: R Boot Camp: the very basics
10:15-10:45 AM break
10:45-11:30 AM Lesson 2: R Boot Camp: working with data frames
11:30-11:45 AM break
11:45 AM-12:45 PM Lesson 3: From linear model to linear mixed model
12:45-2:00 PM lunch break
2:00 PM-4:00 PM office hours

(Day 2 schedule has the same format)

## Lesson 1 learning objectives

At the end of this lesson, students will …

• Know what R is and what it can do.
• Use the R console to interactively issue R commands.
• Know the most common data types in R.
• Know how statistical distributions work in R.
• Know what R packages are and how to install and load them.

## Introduction to R and RStudio

### What is R?

• A statistical programming language
• Users contribute packages
• Free and open-source

### What is RStudio?

• A tool to help you write and run code in R
• RStudio is not R, it is an interface for R (you need to also have R installed to run RStudio)
• We will access RStudio through Posit Cloud for this course
• Or you can run RStudio locally if you prefer

### RStudio panes

• Console: Enter individual lines of code, see output
• Scripts: Edit and run scripts (text files containing code)
• Environment: Shows variables that you have created
• Files/Plots/Help: Includes several tabs
• Plots: display images generated by your R code
• Packages: view and install R packages
• Help: documentation for functions and packages

### The basic moving parts of R

• variable: a structure that holds data. Examples:
• a vector of integers c(1, 2, 3)
• a character string "USDA"
• a data frame with 1000 rows and 10 columns

### The basic moving parts of R

• function: something that takes arguments as input, does something, and returns output.
• log(10): takes a numeric value as input and returns a numeric value as output
• c(1, 5, 6): The function c() takes multiple values as input and returns a vector as output.
• read.csv('myfile.csv'): takes a character string as input and returns a data frame as output.

## How to R

• Let’s start writing our first R code!
• Enter the example code in the console

### Using R as a calculator

• Use operators: +, -, *, /, ^ to use R as a calculator
2 + 3

### The assignment operator

• The assignment operator <- is used to create a new variable and give it a value. The syntax is variable <- <value>.
• Variable names can contain . or _ but can’t contain spaces or start with a number.
• You can also use = as an assignment operator but we will use <- in this workshop. Consistent code is readable code!
x <- 2 + 3
y = 3.5
• Entering the name of a variable prints that variable’s value to the console.
• If you assign a value to a new variable, nothing will print to the console. But the variable is now defined in your environment and can be used later.
x
x + y
x * 4

x <- x + 1
z <- x * 4
z

• Any line preceded by # is a comment and will not be evaluated.
# This is a comment

### Functions with arguments

• A function followed by an argument in parentheses (), like function(<value>), will input a value to a function and return some output
log(1000)

sin(pi)
• Functions can take multiple arguments separated by commas ,
• You can use either 'single quotes' or "double quotes"
my_name <- "Quentin"

paste('Hello,', my_name)

### Getting help

• Use ? to get help about a function
?paste
• Use ?? to search all help documentation for a term
??sequence

## Types of output

• Usually output prints to the console unless assigned to a variable
• Some code produces other output as a “side effect,” such as a plot
plot(mpg ~ hp, data = mtcars)

## Errors, warnings and notes

• Errors
• Warnings
• Notes

### Errors

• Indicates something went wrong
• No output is produced
sin(pi))

### Warnings

• Indicates the result may not be what you expected
• Code still runs and produces output
log(-5)
• Just a note. Everything is still fine!
rep(0, 100000)