R Part 1 - Getting Started

Dave White

Install

R install
Windows
https://cran.r-project.org/bin/windows/base/

Mac
https://cran.r-project.org/bin/macosx/

Linux
https://cran.r-project.org/bin/linux/

Rstudio install
https://www.rstudio.com/products/rstudio/download/#download

These BOTH need to be installed.

Running R

In MacOS or Linux terminal type

R

bash
In windows cmd.exe type

R.exe

bash

Opening R this way opens the REPL…

REPL

REPL = read eval print loop

down history = ctrl-n or down
up history = ctrl-p or up
tab-completion
clear view = ctrl-l
quit = ctrl-d
newline = ctrl-c

help("data.frame") # help on data.frame
help("ls")
# up and down or j and k to scroll
# q to quit
ls # gives code for function

builtins()
page(builtins)

grep("ls",builtins(),value=TRUE)

# save variables
ls()
save("myvars")
rm(list=ls())
ls()
load("myvars")
ls()

#save history
history()
q() # quit
history()
loadhistory()

savehistory(file="myhistory")
q()
loadhistory(file="myhistory")
history()

Syntax highlighting (Don't do during tutorial)

install.packages("devtools")
devtools::install_github("jalvesaq/colorout")
library("colorout")

R-studio

4 pannels

Find the following tabs

  1. Console (command prompt)
  2. Help
  3. Environment
  4. Editor

Very useful:
Help selection menu (not panel) -> cheat sheets

Overview

Philosophy

MATLAB - private inter-workings, super user-friendly interface
R - viewable inter-workings, user-friendly interface, more features

R at first glance

Intro

For me, "Dataframe Oriented"
Feels between matlab and R

Weirdness about R

Goodness about R

Bad

Generally Slowest (besides Octave)

Assignment Operators

A <- 3
A.B <- 3
remove(A)  # unbind/delete
print(A.B) # query contents of A.B
4 -> A
print(A)

a <- b <- c <- d <- 2 # chain assignment
print(d)
print(a)

A = 3
A.B = 3
rm(A)
print(A.B)
print(A)

sample(1:4, 10, replace=TRUE)

Conventions

<- for variables
= for arguments

= used to not work for variable assignment

Mathematical Operators

# Anything after a hash sign is a comment - it will not be evaluated
8+5                          # + is an operator
8*5                          # Multiplication
(5+3)/2                      # Order of operations apply!
a <- 2^2
a^2

Logical Operators

b3
a=-4
b > abs(a)                   # Is b greater than abs(a)?
b < abs(a)                   # Is b less than abs (a)?
                             # Is b equal to 3? Notice to equal signs instead of 1.
b != 3                       # Is b not equal to 3?
b <= 4                       # Is b less than or equal to 4?
b >= 4                       # Is b greater than or equal to 4?
TRUE==1                      # True and 1 are are the same
FALSE==0                     # False and zero are the same
!TRUE==FALSE                 # ~ takes the compliment of a test (flips the sign)

c <- b==4                    # Assigning result of logical test
print(c)

Combined logic

!(b <= 4)                    # Is b less _greater_ than or equal to 4?
b==5 | abs(a)==3             # OR
b==5 & abs(a)==3             # AND
(b==3 & abs(a)==3 ) | c      # Grouping

Enviornment

environment
bindings existing in a namespace
global environment
workspace
dropdown shows what exists globally
more on this later