workshops

R: Fundamental Concepts with RStudio

What is this series about?

A high-level data analysis workflow

Import (data) ➡️ Tidy (data) ➡️ Visualize (data) ➡️ Communicate (findings)

Where we are in the series

  1. R: Fundamental Concepts with RStudio

Why R: supported and open source

Why R: reproducibility and replicability

Term Meaning
Reproducibility Same data, same methods
Replicability New data, same methods

Most scientists report failing to reproduce others’ results, and many their own. Survey of 1,576 researchers (Baker, 2016, *Nature*).

Motivation 🤔

R vs. RStudio

Today’s Goals

By the end of this session, you should be able to:

RStudio

Installing packages

# install.packages("dplyr") # This call installs a package 
# library(dplyr) # This loads a package
# devtools packages
# install.packages("devtools")
# install_github("username/repositoryname")

Simple Math

2 + 2 # add
## [1] 4
3 - 2 # subtract
## [1] 1
3 * 3 # multiply
## [1] 9
4 / 2 # division
## [1] 2
sqrt(9) # square root
## [1] 3

Functions

# c() is a common function for concatenating things together
c(1:10)
##  [1]  1  2  3  4  5  6  7  8  9 10

Variables

Creating variables

# You can then recall the values (object) associated with your variable...
my_variable <- c(1:10)
myAlphabet <- letters[1:5]
myNumbers <- c(1:5)
#And plug it into functions, ie, do computations on it...

my_variable
##  [1]  1  2  3  4  5  6  7  8  9 10
my_variable * 2
##  [1]  2  4  6  8 10 12 14 16 18 20

Naming variables

# camel case
myNumber <- "Hello" # bad naming
myNumber <- 10 # good naming
# skeleton case
my_number <- 10

Data types

Boolean (Logical Data)

myBool <- TRUE # this is camel case
typeof(myBool)
## [1] "logical"

Numeric (Double)

myDouble <- 10.5
typeof(myDouble)
## [1] "double"

Numeric (Integer)

myInteger <- 10L
typeof(myInteger)
## [1] "integer"

Characters

myChar <- "a" # Note that R does not distinguish between '' and "" but the use has to match
typeof(myChar)
## [1] "character"

Complex

myComplexNum <- 9 + 3i # remember sqrt(-1) :)
typeof(myComplexNum)
## [1] "complex"

Data Structures 🗂️

Vectors

# numeric vector
c(1:10)
##  [1]  1  2  3  4  5  6  7  8  9 10
# character vector
c("a", "b", "c")
## [1] "a" "b" "c"
# logical vector
c(TRUE, TRUE, FALSE)
## [1]  TRUE  TRUE FALSE
# note that vectors do not allow you to mix types
# c(TRUE, 10) # this will convert TRUE to 1
# c(TRUE, "10") # this will convert TRUE to "TRUE"

Matrices

matrix(1:10, nrow = 2)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    3    5    7    9
## [2,]    2    4    6    8   10

Data frames

data.frame(letter = letters[1:5],
           nol = 1:5) # nol mean number of letter
##   letter nol
## 1      a   1
## 2      b   2
## 3      c   3
## 4      d   4
## 5      e   5

Lists

list(item_1 = myAlphabet,
     item_2 = c(1:3))
## $item_1
## [1] "a" "b" "c" "d" "e"
## 
## $item_2
## [1] 1 2 3

Arrays

my_array <- array(1:12, dim = c(2, 3, 2)) # 2 rows, 3 columns, 2 layers
my_array
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]    7    9   11
## [2,]    8   10   12

Identifying Data Structures

class(c(1:10))
## [1] "integer"
class(matrix(1:10, ncol = 2))
## [1] "matrix" "array"
class(data.frame(var_1 = letters,var_2 = 1:26))
## [1] "data.frame"
class(list(item_1 = letters,item_2 = 1:10,item_3 = c(TRUE, FALSE)))
## [1] "list"

Resources: getting help

  1. Forums (e.g., Stack Overflow or Posit Community)
  2. Blog posts and online tutorials (search for ‘blogdowns’ on a topic)
  3. Package papers (often Journal of Statistical Software)
  4. Books, many free online (see References)

Resources: the ? operator (and vignettes)

?sqrt                    # help page for a function
??regression             # search all help pages for a topic
browseVignettes("dplyr") # long-form guides for a package

References

And through the library, the O’Reilly platform hosts a plethora of titles related to R.