2022-11-23

Basics of R

This workshop serves as a primer to R by introducing the basics. You may follow via the .rmd file within RStudio rather than solely the compiled PDF. This way you can experiment with code in the “code blocks” provided.

Note: In text R code is displayed in a fixed-width font.

R Markdown

This is an R Markdown presentation. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.

What is R?

R is a system for statistical computation and graphics. It consists of a language plus a run-time environment with graphics, a debugger, access to certain system functions, and the ability to run programs stored in script files.

Why is R named R?

The name is partly based on the (first) names of the first two R authors (Robert Gentleman and Ross Ihaka), and partly a play on the name of the Bell Labs language ’S

Why use R

  • It’s free (and will always be)
  • Powerful (you’ll be able to perform complex analysis and plots)
  • Flexible (18747 available packages in CRAN repository)

R, as a Calculator

The first thing to know about R is that it is essentially a large calculator capable of performing arithmetic:

1 + 1
## [1] 2
8 * 8
## [1] 64
2 ^ 8 # exponent
## [1] 256

R, as a Calculator

(5 + 2) ^ 4
## [1] 2401
5 + 2 ^ 4
## [1] 21

R also supports elementary and algebraic functions:

log(100)
## [1] 4.60517
sqrt(49)
## [1] 7

Order of Operations

R solves equations according to the order of operations, “PEMDAS”:

  1. Parentheses
  2. Exponents
  3. Multiplication
  4. Division
  5. Addition
  6. Subtraction

Chalenge:

Try this! Using R, solve: (5 + 1) ^ 4 / (9 - 2) ^ 3

Objects

R is an “object oriented” programming language. Put simply, R uses objects to store attributes. Objects are created by assigning an attibute to them via the <- operation. You can always view the attribute of an object by typing the object name.

object1 <- 10 + 10 
object1
## [1] 20

Try this! Create a new object; you can name it almost anything!

R includes various functions for managing created objects.

The ls() function lists all existing objects.

ls()
## [1] "object1" "object2"

The rm() function removes existing objects.

rm(object1)

Naming Objects

There is no strict convention for naming objects; however, there are best practices:

  1. Avoid spaces; use underscores, periods, or CamelCase (or camelCase) for long object names
    • e.g., This_is_a_long_name, This.Is.A.Long.Name, thisIsALongName
  2. Avoid names of existing functions or reserved R objects
    • e.g., mean or sum
  3. Be descriptive but keep it short (less than 10 characters)
  4. Avoid special characters
    • e.g., ? $ % ^ &
  5. Numbers are fine, but names cannot begin with numbers.
    • e.g., object1 is ok, but 1object is not

Naming Objects

Important: Object names are case sensitive. - e.g., object.One and Object.One refer to two separate objects

object.One <- 10 + 10
Object.One <- 5 + 5
object.One
## [1] 20
Object.One
## [1] 10

Functions

In addition to elementary and algebraic functions, R includes functions that simplify statistical analysis. For example, the mean() function calculates the mean.

Note: Sometimes na.rm = TRUE is necessary within the paranetheses to instruct R to ignore missing data.

mean(cars$dist, na.rm = TRUE)
## [1] 42.98

A note about syntax: the dollar sign, $, is used to indicate the variable of interest relative to a data set: R calculated the mean using the dist variable within the cars data set by specifying cars$dist

Object Types

The R str() function describes the structure of objects and functions.

str(mean)
## function (x, ...)
str(object.One)
##  num 20
str(cars)
## 'data.frame':    50 obs. of  2 variables:
##  $ speed: num  4 4 7 7 8 9 10 10 10 11 ...
##  $ dist : num  2 10 4 22 16 10 18 26 34 17 ...
str(cars$dist)
##  num [1:50] 2 10 4 22 16 10 18 26 34 17 ...

Vectors

In R, vectors are defined as a collection of data of the same type. The c() function creates a vector. There are different type of vectors

nums <- c(-2,-1,0,1,2) #V. numerical
str(nums)
logic <- c(T,F,T,T,F) #V. logic
str(logic)
Names <- c("Travis", "Smith", "Sintson", "Brooke", "Stone") #Strings
str(Names)
length(Names) #??

Note: Beware of mixing types of data:

nums.names <- c(nums, Names)
str(nums.names)

Vectors

Special Values:

NA (Not Available), NaN (Not a Number), Inf e –Inf (infinite)

Z <- c(NA, 1, 2)
Z
## [1] NA  1  2
log(nums) #log() logarithimic function
## Warning in log(nums): NaNs produced
## [1]       NaN       NaN      -Inf 0.0000000 0.6931472
nums * 2
nums >= 1
nums > -1 & nums < 2
!(nums > -1 & nums < 2)

Regular sequences

The : operator:

1:15
15:1

The seq() function:

seq(1,15)
seq(from=15, to=1)
seq(from=2, length=15, by=2)

The rep() functions:

rep(1:3, times = 2)
rep(1:3, each=2)

Try yourself: Define a vector of odd numbers between 13 and 27

Factors

Gender <- c("F", "M", "M", "F", "M") #Strings
Gender <- factor(Gender)    #convert to factor
str(Gender) # levels as numbers
##  Factor w/ 2 levels "F","M": 1 2 2 1 2
Names[Gender=="M"]
## [1] "Smith"   "Sintson" "Stone"
levels(Gender) <- c("Female", "Male")   #levels labels
str(Gender)
##  Factor w/ 2 levels "Female","Male": 1 2 2 1 2

Indexing vectors

Names
Names[2]
Names[c(1,3)]
Names[-c(1,3)]
Names[logic]
nums[!(nums > -1 & nums < 2)]
Names[!(nums > -1 & nums < 2)]
## [1] "Travis"  "Smith"   "Sintson" "Brooke"  "Stone"  
## [1] "Smith"
## [1] "Travis"  "Sintson"
## [1] "Smith"  "Brooke" "Stone" 
## [1] "Travis"  "Sintson" "Brooke" 
## [1] -2 -1  2
## [1] "Travis" "Smith"  "Stone"

Matrices

Matrix objects are generalization vectors for 2 dimensions. Matrices have the same data type (mode) and are defined by the number of rows and columns.

M <- matrix(1:12, nrow=3, ncol=4, byrow=FALSE)
M
dim(M) #returns the dimensions M

A <- 1:20
dim(A) <- c(5, 4) #Converts A to a matrix 5x4
A
as.vector(A) #back to vector

rbind(nums,nums)        #By rows
cbind(A,nums)       #By colums 

Indexing (similar to vectors, but need a index for rows and a second for columns:

M[3,2]      
M[3, ]  
M[ ,2]      
M[-2,c(1,4)]    #??

Lists

Lists are another generalization of vectors. An order collection of objects that can have different type and number of elements.

Greyling <- list(Latin_Name = "Thymalus thymalus", family = "Salmonidae", 
                 length_cm = rnorm(100, 30, 10), 
                 rivers = c("Rhine", "Danube", "Neckar"), 
                 Food_itens = matrix(rpois(20*3,2), nrow = 20, ncol = 3, 
                                     dimnames = list(NULL, c("Insecta", "Crustacea", "Plants"))))
Greyling
str(Greyling)

Greyling[[2]] #indexing by position (double square brackets)
Greyling$Latin_Name #indexing by name

Greyling$max_lenght <- max(Greyling$length_cm) #Create new element

Data Frame

A particular case of lists with all components with the same number elements or a generalization of matrices with columns with different type of data.

str(chickwts)
head(chickwts)      #see fist rows
chickwts[,2]        # Matrix notation
chickwts[[2]]       # List notation
chickwts$feed       # List notation

Data frames are the working standard in R

Data Frame

Try: Using the data frame chickwts

  1. How many records has the table?
  2. How many food supplements were tested?
  3. What are the weights of chickens that were fed on “linseed”?
  4. How many supplements were given to chickens over 350 g?
  5. What are the weights and supplements given to chickens under 160g?
  6. What are the weights and supplements given to chickens fed on “soybean” or “linseed”

Packages

Packages expand R to include additional functions important to statistical analysis.

Installing Packages

Installing packages in R can be performed via install.packages("packagename"), whereby the name of the desired package must be within quotation marks.

Note: Occasionally a package may require dependencies for installation. The dependencies can be automatically installed along with the package by including the dependencies = TRUE argument within the install.packages() function.

Try Use this code to install the following package: tidyverse

Packages

The tidyverse is an collection of R packages designed for data science that share design philosophy, grammar, and data structures.

Loading Packages

After installation, packages must be loaded to use their functions within R. Packages are loaded via library(packagename). Note: The library() function does not require quotation marks around the package name.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()

R Help

R includes a help function to assist with functions, accessible by including a ? prior to the function name.

?mean

Note: The help documentation will display in the bottom right quadrant of RStudio. Alternatively, typing the function name into the help search bar will yield a similar result.

To search all of R documentation for help about a function, use ??.

??mean

Note: Google is a valuable tool for finding help. Large communities like StackOverflow provide answers and explanations to common issues in R. At times, a particular problem may seem unique, but someone else has almost certainly had the same problem and the solution likely can be found online.

Setting a Working Directory

The working directory is the location where files are accessed and saved within a R session. Normally, the working directory is set at the beginning of every R file. The working directory should be set and the class data loaded at the beginning of each session.

There are two methods of setting the working directory. First, the setwd() function can be used with the directory path. For example, setwd("C:/Directory/_to/_folder/").

Second, within RStudio, the “Session” tab will allow you to set the working directory.

The getwd() function returns the set working directory.

getwd()
## [1] "H:/My Drive/Baden-Wutemberg Fish/Intro-R"

Import Data!

R can read many different file types, including text files, Excel files, Google Sheet files, SPSS files, and Stata files. It can even read data sets directly from websites. The file type determines the function that is necessary import a data set. For example, CSV files use the function read.csv to import a dataset. Here is an example that uses this function:

ds <- read.csv("Class Data Set Factored.csv", header = TRUE)

This line of code saves the data set in Class Data Set Factored.csv to an object called ds (short for data set). The header = TRUE argument tells R that the first row in the data set provides column (or variable) names.

Note: It’s is also possible there’s a necessity to define the separator with the sep argument as follows sep = ";" or sep = ",".

Try your own table!