Intro to R Part 1:

Getting Started


Andy Lyons
October 11, 2022

https://ucanr-igis.github.io/IntroR_Oct22/



About Me…

About You

Number of registrants: 89

Familiarity with R

Email domain

Location

Goal

Move in the direction of becoming functional with R!!


Learning Strategy

1) Understand foundational terms and concepts

2) Hands-on practice

3) Discover RStudio’s bells and whistles

4) Learn how to get help


Today’s Focus

Learning Tips

1) Watch now, practice later

2) Review what we cover within 24 hours


Workshop Series

Date Session
Oct. 11, 2022
3:30p - 5:00p
Part 1. Getting Started
Oct 12, 2022
3:30p - 5:00p
Part 2. Packages, Piping, and Importing Data
Oct 14, 2022
10:30a - 12:00p
Part III. Data Wrangling


See also Getting Started with R resources.

R and RStudio

Why is R So Popular?

  1. It’s free!

  2. Huge user community (especially academics)

  3. Thousands of add-ons (packages) that extend its capabilities

  4. Particularly strong in plotting and reporting

  5. Once you get over the initial hump, can work very efficiently

  6. Makes it easy to get your code “out there”

  7. Solid overall programming language


Exercise 1: RStudio Exploration and Basic Commands

Exercise 1 Topics

  1. Using R like a fancy calculator
  2. Order of operations
  3. Comparison operators
  4. Saving the results of expressions to variable
  5. Rules for naming variables

RStudio Cloud project for this workshop:

https://rstudio.cloud/content/4701501

After it opens:


Break!

Exercise Review

Key vocabulary terms are in italic.

Naming Objects

The rules for naming objects are pretty flexible. You can use numbers, letters, and most special characters.

A few rules to take note of:


Naming Styles

There are a handful of popular naming styles. Pick one that you like, and be consistent!

Style Example
alllowercase adjustcolor
period.separated shoe.size
underscore_separated (aka snake case) numeric_version
lowerCamelCase addTaskCallback
UpperCamelCase SignatureMethod

Data Types

All variables have a class or data type, which you can view using class().

num_plots = 10
class(num_plots)
## [1] "numeric"

Other common data types:

Vectors

vectors are R objects that contain multiple values of the same class.

Example:

i = 4:12
i
## [1]  4  5  6  7  8  9 10 11 12


More examples:


Creating Vectors

In general, you need to use a function or operator to create a vector.

Sequence of numbers with the : operator:

1:10
##  [1]  1  2  3  4  5  6  7  8  9 10


Repeat function:

rep("Quercus lobata", 5)
## [1] "Quercus lobata" "Quercus lobata" "Quercus lobata" "Quercus lobata"
## [5] "Quercus lobata"


Combine elements of the same class with c():

yn <- c(TRUE, FALSE, TRUE)
yn
## [1]  TRUE FALSE  TRUE


Some built-in constants are also vectors:

LETTERS
##  [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
## [20] "T" "U" "V" "W" "X" "Y" "Z"
state.abb
##  [1] "AL" "AK" "AZ" "AR" "CA" "CO" "CT" "DE" "FL" "GA" "HI" "ID" "IL" "IN" "IA"
## [16] "KS" "KY" "LA" "ME" "MD" "MA" "MI" "MN" "MS" "MO" "MT" "NE" "NV" "NH" "NJ"
## [31] "NM" "NY" "NC" "ND" "OH" "OK" "OR" "PA" "RI" "SC" "SD" "TN" "TX" "UT" "VT"
## [46] "VA" "WA" "WV" "WI" "WY"
month.name
##  [1] "January"   "February"  "March"     "April"     "May"       "June"     
##  [7] "July"      "August"    "September" "October"   "November"  "December"


Random number functions:

runif(20)
##  [1] 0.24034637 0.36129627 0.31932912 0.36040619 0.10315605 0.66583121
##  [7] 0.15364120 0.02328397 0.62662163 0.46527359 0.09406921 0.43920132
## [13] 0.39845071 0.69523229 0.77945612 0.42430562 0.57525211 0.78171852
## [19] 0.29296575 0.64298070
rnorm(20)
##  [1] -0.2147803  0.3774138  0.6490794 -0.3913319 -0.3312433  0.7447508
##  [7]  0.5856689  0.9039691 -1.0058598  0.3497485  2.1102232 -0.8948942
## [13] -0.9326346 -1.7975214 -0.4048500 -0.9293515  0.4510814 -1.2083119
## [19]  0.3961602 -0.7003346
sample(month.abb, 3)
## [1] "Jan" "Feb" "Apr"

How Vectors Behave

Vectorized operations

Many R functions and math operators are vectorized (i.e., operate on each individual element).


Examples

First we create two numeric vectors:

x = 0:4
x
## [1] 0 1 2 3 4
y = 11:15
y
## [1] 11 12 13 14 15


Are sin() & cos() vectorized?

sin(x)
## [1]  0.0000000  0.8414710  0.9092974  0.1411200 -0.7568025
cos(x)
## [1]  1.0000000  0.5403023 -0.4161468 -0.9899925 -0.6536436


Addition (and all math functions) is vectorized:

x + 1
## [1] 1 2 3 4 5
x + y
## [1] 11 13 15 17 19


Aggregate functions

Functions that accept a vector and spit out a single value are aggregate.

x = runif(20)
x
##  [1] 0.39271638 0.73787278 0.89058362 0.34175404 0.21611711 0.86796133
##  [7] 0.84449887 0.54382415 0.79583754 0.02044313 0.67371211 0.89399457
## [13] 0.50483813 0.81232642 0.53471147 0.60363816 0.18086160 0.78866131
## [19] 0.11792667 0.84987164


Most descriptive stats functions are aggregate:

mean(x)
## [1] 0.5806076
median(x)
## [1] 0.6386751
sd(x)
## [1] 0.2826853


Other aggregate functions:

first(state.name)
## [1] "Alabama"

Subsetting Vectors

To extract a single element from a vector, use square bracket notation. Inside the square brackets, put the index of the element(s) you want.


Subset with indices


LETTERS[2]
## [1] "B"

To return multiple elements, pass a vector of indices.

LETTERS[2:4]
## [1] "B" "C" "D"

You can also use square brackets to extract elements in a different order.

LETTERS[4:2]
## [1] "D" "C" "B"


Subset with logicals

You can also insert a vector of Logical values (TRUE/FALSE) in the brackets. R will return the corresponding element for the TRUE values.

LETTERS[c(T,T,T,T,T,T,T,T,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F)]
## [1] "A" "B" "C" "D" "E" "F" "G" "H"


Better still, use an expression that returns a vector of logical values:

state.abb[ substr(state.abb, 1, 1) == "N" ]
## [1] "NE" "NV" "NH" "NJ" "NM" "NY" "NC" "ND"


Plotting Vectors

Base R has simple plotting functions you can use to view the distribution of data.


Histograms

To make a histogram, use hist():

my_vals = rnorm(500)
hist(my_vals)


Box Plots

Prefer a box plot?

boxplot(my_vals)


Scatter Plots

The versatile plot() can be used to make a simple scatter plot:

plot(x = 1:20, y = runif(20))


Line Plots

plot(x = 1:20, y = runif(20), type = "b")


Scripts

Top five advantages of using scripts over the console:

  1. Easier to write (and fix!) your code
  2. You can add comments to remind yourself what each command is doing
  3. Reuse your own code
  4. You can add loops and if-then statements later on
  5. Tell your friends you’re a coder!

Exercise 2: Working with Scripts and Vectors

Exercise 2 Topics

  1. Saving code in scripts
  2. Data types
  3. Vectors

Exercise 2 Review


END!