November 2015

Program

Monday december 7: R Basics

  • What is R
  • The R GUI, Workspace
  • Objects, operators, functions
  • Basic Analyses in R

Monday december 14: Getting Started with Data Analysis in R

  • Loading and exporting datafiles
  • Installing and using R packages
  • Advanced analyses in R

What is R?

R

  • R is a language and environment for statistical computing and for graphics

  • GNU project

  • Managed by the R Foundation for Statistical Computing, Vienna, Austria.

  • Based on the object-oriented language S (1975)

R is Awesome

  • Free

  • Open Source

  • Community Driven

  • Many resources available

The help resources

  • Everything on the Comprehensive R Archive Network (CRAN) and is aimed at R users, must be accompanied by a comprehensive help file. - Type ?anova or help(anova) in the console.
  • Type ?? followed by your search criterion. For example ??anova returns a list of all help pages that contain the word 'anova'
  • Alternatively, the rest of internet will help. For example: http://www.stackoverflow.com, http://www.stackexchange.com
  • If you google R related issues; use 'R:' as a prefix in your search term

The R Graphical User Interface (GUI)

How does R work

First things first: R as a basic calculator

  • Operators: built in functions to do the most basic operations

First things first: R as a basic calculator

  • Operators: built in functions to do the most basic operations

    • Arithmetic operators: +, -, /, *, ^, :
    • Logical operators: <, <=, >, >=, ==, !=
(2+3)/5
## [1] 1
2>3
## [1] FALSE
2/3==4*2/12
## [1] TRUE

Objects, operators, functions

  • Store data in "objects"
  • Store "functions" in objects
  • Perform calculations on objects with data, using "operators" or functions
  • These operators and functions return results
  • which can in turn be stored in objects.

Where do you get these objects, operators, functions?

  • You program them yourself
  • They are stored as part of basic R functionality
  • They are stored in packages made for R that you have installed

Objects

Putting Data in Objects:

  • R works with objects that consist out of elements (data).
  • The elements are 'assigned' to objects.
  • Once you have assigned an element to an object, it is stored in the global environment.
  • Assigning things in R is very straightforward:

    • you just use <- (or =)
a <- 100

Calling Objects

  • Calling things in R is also very straightforward:

    • you just use type the name you have given to the object
  • For example, we assigned the value 100 to the object called a. To call object a, we would type

a
## [1] 100

Types of Data to put in Objects (modes)

  • Numeric (numbers)
  • Character or String (text)
  • Logical (True or False)
  • Many other (sub)categories which are beyond the scope of this course.
a <- 100
mode(a)
## [1] "numeric"
b <- "apple"
mode(b)
## [1] "character"

Types of Data to put in Objects

c <- "100"
mode(c)
## [1] "character"
d <- as.numeric(c)
d
## [1] 100
e <- 3>2
mode(e)
## [1] "logical"

Special classes of Data to put in Objects

  • Missing data is coded as NA.
  • Data that are the result of ´impossible' operations are coded as NaN
f <- NA
is.na(f)
## [1] TRUE
sqrt(-1)
## Warning in sqrt(-1): NaNs produced
## [1] NaN

Types of Objects for Storing Your Data

Types of Objects for Storing Your Data

  • vectors
  • matrices
  • arrays (beyond the scope of this course)
  • lists
  • dataframes

    • Each type of object can only store either numeric data, or string data (but NA and NaN may be mixed in)
    • with the exception of dataframes and lists.

Vectors

  • A vector is a 1-dimensional concatenation of one or more elements (either all strings, or numbers)
a <- c(1, 2, 3, 4, 5)
a
## [1] 1 2 3 4 5
b <- 6:10
b
## [1]  6  7  8  9 10

Calling elements in vectors

If we would want just the third element, we would type

b[3]
## [1] 8

Matrices

  • A matrix contains one or multiple vectors
  • a matrix is 2-dimensional: nrow specifies the number of rows, ncol the number of columns
c <- matrix(a, nrow = 5, ncol = 2)
c
##      [,1] [,2]
## [1,]    1    1
## [2,]    2    2
## [3,]    3    3
## [4,]    4    4
## [5,]    5    5

Calling elements in matrices #1

  • The first row is called by
c[1, ]
## [1] 1 1
  • The second column is called by
c[, 2]
## [1] 1 2 3 4 5

Calling elements in matrices #2

  • The intersection of the first row and second column is called by
c[1, 2]
## [1] 1

In short; square brackets [] are used to call elements, rows, and columns

Dataframes

  • The object type that R uses for fitting statistical models to
  • Look and work similar to matrices
  • Contain multiple vectors, organized in rows and columns
  • May contain both vectors with characters, and vectors with numbers!
head(ToothGrowth)
##    len supp dose
## 1  4.2   VC  0.5
## 2 11.5   VC  0.5
## 3  7.3   VC  0.5
## 4  5.8   VC  0.5
## 5  6.4   VC  0.5
## 6 10.0   VC  0.5

Lists

  • A list is an object that contains a collection of other objects.
l = c(2, 3, 5) 
m = c("aa", "bb", "cc", "dd", "ee") 
x = list(l,m)   
x
## [[1]]
## [1] 2 3 5
## 
## [[2]]
## [1] "aa" "bb" "cc" "dd" "ee"
  • Most R model fitting functions return the analysis results in lists.

Functions

Functions

  • functions are objects that are used to do calculations on other objects
  • all functions have a name, and they can be called using that name.

Functions

  • functions are objects that are used to do calculations on other objects
  • all functions have a name, and they can be called using that name.
  • if you want to know how a function works and what it does, call ?functionname
?mean()

Functions

  • all functions work by specifying one or more 'arguments' for that function (like setting options)
x <- c(2,4,3,NA,5)
mux <- mean(x, na.rm=TRUE)
mux
## [1] 3.5

Functions

  • Many basic functions are available in R from the start. You have already seen these: mean(), c(), matrix(), list(),head(),mode(), as.numeric(), is.na(), sqrt()
  • Many more are available in packages
  • And you can make new ones yourself

Practical

Tips

  • use the help resources available online
  • Don't be afraid to just try things
  • Save your code regularly