0% found this document useful (0 votes)
4 views56 pages

R Programming Course Overview

Uploaded by

djha49238
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views56 pages

R Programming Course Overview

Uploaded by

djha49238
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Course overview and introduction

21.04.2022

Danny Arends
Fachgebiet Züchtungsbiologie und molekulare Tierzüchtung
Humboldt-Universität zu Berlin
Overview

 Today
 General course announcements
 What you will be able to do (after the whole course)

 Introduction into R

 Assignments to practice
General course announcements

 Lecture slides and assingments will be made available online


on Moodle and my own website

 Please attend the lectures on YouTube (ask questions)

 Lectures are supported by practical exercises, spend some


time on them, if you get stuck: Send me an email
 In person meeting before the lecture ?

 Exam date unknown at this point in time (most likely: 21st Juli)
 Form: Online exam by written examination
Questions to you

 Why did you decide to follow the


course ?
 Any prior statistics courses ?
 Any programming experience ?

 What do you want to learn ?

 Do you have a data set you are


currently working on ?

Image by Anemone123 from Pixabay


What you will be able to do
(after the whole course)

 Format your data for R


 Load your data in R
 Use statistics on your data to answer questions
 Know how to formulate these questions
 Interpret the results from statistics
 Create your own custom plot
 Suitable for publications
 Create your own analysis
 Write functions
 Publish your work as an R package for others to use
What you will be able to do
(after the whole course)

 Analyze your own data


 Line and dot plots
 Boxplot
 Histogram
 Heat maps
 Contour
 Your own
What you will be able to do
(after the whole course)

 Analyze your own data


 Line and dot plots
 Boxplot
 Histogram
 Heat maps
 Contour
 Your own
What you will be able to do
(after the whole course)

Histogram of x
 Analyze your own data

0.20
 Line and dot plots
 Boxplot

0.15
 Histogram
 Heat maps
Density

0.10
 Contour
 Your own 0.05
0.00

0 5 10 15

x
What you will be able to do
(after the whole course)

 Analyze your own data


 Line and dot plots
 Boxplot
 Histogram
 Heat maps
 Contour
 Your own

0.0 0.2 0.4 0.6 0.8 1.0

Art
Introduction into R
21.04.2022

Danny Arends
Fachgebiet Züchtungsbiologie und molekulare Tierzüchtung
Humboldt-Universität zu Berlin
Overview

 History
 Look and Feel of R
 R as a calculator
 Types
 Indexing
 Variables
 Scripts
Why history

 Why history in an R course ?

“We are like dwarfs on the shoulders of giants, so that we can see more
than they, and things at a greater distance . . . we are carried high and
raised up by their giant size”

- John of Salisbury

From: [Link]
History
 2400 BC Abakus (Babylon)
 1110 BC Differential gears / south-pointing chariot (China)
 200 BC Chinese Abakus (China)
 120 BC Antikythera mechanism (Greece / Korinthe)

1) Abacus, public domain by its author, HB


2)Andy Dingley, CC BY 3.0 <[Link] via Wikimedia Commons
3) Antikythera mechanism, public domain by its author
History
 100 Astrolabe (Unknown)
 800 Crypto, frequency analysis (Alkindus, Irak)
 1206 Castle clock (Al-Jazari, Mosul, Irak)
 1673 Leibniz wheel (Gottfried W. Leibniz, Germany)

“it is beneath the dignity of excellent men to waste their


time in calculation when any peasant could do the work
just as accurately with the aid of a machine.”
- Gottfried Wilhelm Leibniz

Christoph Bernhard Francke, Public domain, via Wikimedia Commons


History
 Charles Babbage
– 1791 - 1871
– Analytical Engine

 Ada Lovelace
– 1815 - 1852
– First algorithm

 Alan M. Turing
– 1912 - 1954
– Turing Machine

Images from: Wikimedia commons


CC BY 3.0 <[Link] via Wikimedia Commons
Turing machine

 Abstract machine
 Symbols on a strip of tape
 Table of rules for manipulation

 Any computer algorithm can


be implemented by a Turing
machine

 Computational complexity
theory
Top: Projet Rubens, ENS Lyon
Bottom: GabrielF
CC BY 3.0 <[Link] via Wikimedia Commons
History

 Build May 1941


 Z3 - The first computer
 Plankalkül
 Prof. Dr. Konrad Zuse

 Wing flutter
 Destroyed 1943

(1) Zuse Z1 replica in the German Museum of Technology in Berlin


(2) Konrad Zuse working in his lab (unknown)
History
 ENIAC – Electronic Numerical Integrator And Computer
 Electronic general-purpose computer
 Turing-complete
 Digital
 Capable of being reprogrammed
 loops, branches and subroutines

 Ballistics tables

Programmers Betty Jean Jennings (left) and Fran Bilas (right) operate
ENIAC's main control panel at the Moore School of Electrical
Engineering. (U.S. Army photo from the archives of the ARL Technical
Library)
History

 Von Neumann architecture


 1945 - John von Neumann

(1) von Neumann Architecture - licensed under the Creative Commons Attribution-Share Alike 3.0
(2) Los Alamos: Beginning of an era, 1943-1945, Los Alamos Scientific Laboratory, 1986
Present / Future

 1976 Quantum information theory


Roman Stanisław Ingarden
 1985 Universal quantum computer
David Deutsch
 1991 Entanglement secure communication
 2011 D-Wave One (128 qubits)
 2020 D-Wave Advantage™ (>5000 qubits)
D-Wave One (128 qubits)

 Discrete optimization
 $10.000.000
 ENIAC then: $500,000
now: $6,100,000

 Lattice protein folding

Images from: [Link]


D-Wave Advantage

 Menten AI
 Protein design
 Volkswagen
 Paint shop scheduling
 Save-On-Foods
 Grocery optimization
 Amazon
 Used to? Free access
for CoVID19 researchers

Image from: [Link]


History - programming languages

 Low level
 Punch cards
 Assembly
 Mid level
− C - Dennis Ritchie and Ken Thompson (69 - 73)
 High Level
 Plankalkül - Konrad Zuse (43 - 45)
 AutoCode - Alick Edwards Glennie (50s)
 Algol - Committee of American and European computer scientists
(Late 50s)
 LISP - John McCarthy (58)
 COBOL - Grace Hopper (59)
Too many to count, so why R?

From: [Link]
The R programming language

 Free and Open-Source


 Statistical computing
 Graphics
 Written in:
 R, Fortran, C
 Operating System agnostic
 Linear and non-linear models
 Build-in testing and help
 Many add-on packages available: 4000+
Look and Feel
Look and Feel
Look and Feel
Look and Feel
R as a calculator

 We can use R as a basic calculator:


 So typing in: 1 + 4 will yield 5
 5 / 10 will yield 0.5

 Some special operations:


 Exponents: 5 ^ 2 or 5 ** 2
 Euclidean division %/%
 Euclidean division remainder %%

 Some special numerical constants:


 Inf = infinite, NaN = Not A Number, NA = missing
Euclidean division
100 divided by 39

39 / 100 \
Euclidean division
100 divided by 39

39 / 100 \ 2
78 Euclidean divisor

---
22 Euclidean division remainder
R as a calculator

 Build in character constants:


 LETTERS
 The 26 upper-case letters of the Roman alphabet
 letters
 The 26 lower-case letters of the Roman alphabet
 [Link]
 The three-letter abbreviations for the English month names
 [Link]
 The English names for the months of the year
 pi
 The ratio of the circumference of a circle to its diameter
R as a calculator

 Imaginary numbers
 sqrt(-1) = NaN, but sqrt(-1+0i) = 0+1i

 Additionally basic trigonometry functions


 sin, cos, tan, asin, acos, atan

 Logarithms
 log(5) natural logarithm of 5
 log10(5) base 10 logarithm of 5
 exp(1) e^1
Order of operations

 Operator precedence
 exponents and roots
 multiplication and division
 addition and subtraction

 PEMDAS
Please Excuse My Dear Aunt Sally

 10 - 3 x 2 = ???
Your session

 Everything in R is in RAM memory


 Functions to manage your session:
getwd() # Where does R save/retrieve files ?
dir() # What files are there ?
setwd() # Set another place to save/retrieve files
ls() # What is in the current environment ?

[Link]("qtl") # Install package


library("qtl") # Load a package

save(object, file = "[Link]") # Save an object as binary


[Link]("[Link]") # Save the whole environment
q("no") # quit the R session
Help is available !
?functionname # Open the help file for function
??term # Search for a term across

?seq # Open the help for the seq function


?"+" # The + function help page

??obesity # Search anything obesity in R


Types of data

 Logical
TRUE, FALSE

 Numeric
5, 7.9, 100.6

 Character
"one", "two", "three"

 Vector
v1 <- c(1, 2, 5.3, 6, -2,4) # Numeric vector
v2 <- c("one", "two", "three") # Character vector
v3 <- c(TRUE, TRUE, TRUE, FALSE) # Logical vector

 Matrix
Y <- matrix(1:20, nrow = 5, ncol = 4) # 5 x 4 numeric matrix
Working with types

 Some useful functions to deal with types


length(object) # Number of elements

str(object) # Structure of an object


class(object) # Class or type of an object

[Link](object) # Force to a numeric


[Link](object) # Force to a numeric

[Link](object) # Is of a certain type ?


Creating vectors & matrices

 Vector
c(object, object, ...) # Combine objects into a vector
seq(from, to, by) # A numerical sequence
rep(object, times) # Repeat an object / number

 Matrix
matrix(object, nrow, ncol) # Create a matrix from a vector
cbind(object, object, ...) # Combine objects as columns
rbind(object, object, ...) # Combine objects as rows

m = matrix("", 10, 10) # Empty character matrix


Creating vectors & matrices

 Vector
v1 <- 1:4 # 1, 2, 3, 4
v2 <- seq(1, 100, 7) # 1, 8, 15, .. 99
v3 <- rep(1, 4) # 1, 1, 1, 1
v4 <- rep("A", 4) # A, A, A, A

 Matrix
M1 <- matrix("", 10, 10) # Empty character matrix
M2 <- matrix(NA, 10, 10) # Empty numeric matrix
M2 <- cbind(v1,v3) # 4 x 2 numeric matrix
M3 <- rbind(v1,v3) # 2 x 4 numeric matrix
Vector and Matrix - Example

 Vector

Index by R
 Matrix
Indexing a vector

 Indexing the a vector called v:

v[5]

v[2:5]

v[c(2:5,8)]
Indexing a matrix

m[1:3, 1]
m[5, 3:6]
m[8, 7]
m[ , 9]
Again: Types of data

 Logical
TRUE, FALSE

 Numeric
5, 7.9, 100.6

 Character
"one", "two", "three"

 Vector
a <- c(1, 2, 5.3, 6, -2,4) # Numeric vector
b <- c("one", "two", "three") # Character vector
c <- c(TRUE, TRUE, TRUE, FALSE, TRUE) # Logical vector

 Matrix
Y <- matrix(1:20, nrow = 5, ncol = 4) # 5 x 4 numeric matrix
Advanced Types

 Data Frame # Not a matrix, can contain multiple basic types


v1 <- c(1, 2, 3, 4)
v2 <- c("red", "white", "red", NA)
v3 <- c(TRUE, TRUE, TRUE, FALSE)
d <- [Link](v1, v2, v3)

 List #Not a vector, can contain anything


w <- list(name="Fred", numbers = v1, age = 5.3)

 Factor
gender <- [Link](c(rep("male",20), rep("female", 30)))

 Comments
# This is a comment, R will ignore this, use me often
Quick Type Test

 What is the type of:


 "TRUE"
 "1"
 1e+11
 0x89
 #FF00AA
 FALSE
 [Link](TRUE)
 [Link](1e+11)
Indexing a List: [[ ]]

w <- list(name = "Fred",


numbers = c(1, 2, 3, 4),
age = 5.3,
matrix = matrix(c(1,0,0,1), nrow = 2, ncol = 2))

w[[1]][1]
w$numbers[c(2,3)]
w[[4]][,1]
w$matrix[1,]
Matrix and [Link] functions

nrow(matrix) # Number of rows


ncol(matrix) # Number of columns

rownames(matrix) # Names of the rows


colnames(matrix)

rownames(matrix) <- c("A", "B", "C") # Set row names


colnames(matrix) <- c("A", "B", "C") # Set column names

# Transpose matrix
t(matrix)

Image from: [Link]


Variables

 Variables are boxes


 You can put things in (use: <- or = )
 You can use the box without knowing what is in it
 We have already seen them:
variables <- 1.5
can <- c(TRUE, FALSE)
have <- c(1, 2, 3, 4)
many <- c(FALSE, FALSE)
names <- -5
Code clean = create scripts

 Make a new directory for the assignments


 Every week, use a new file
 Name the files in a logical way
 Add a header (comment section) to each file
 Name
 Date
 Purpose of the file
 Copyright
 Use a LOT of comments
An example
# Analysis of Hardy-Weinberg equilibrium
#
# copyright (c) 2015 - HU Berlin
# written by: Danny Arends
#
# last modified Apr, 2015
# first written Feb, 2015

setwd("D:/Rcourse/assignments")
Use a good text editor

 Windows Notepad++ ([Link]


 OSX BBEdit ([Link]
 Linux Use what you want

 Most important
 Code highlighting
 Support for bracket testing
Remember

 Clean code = smart code


Quest ions ?

Ions by lluvia ramos [Link]


Wizard Hat - New Horizon & Interactive Studios - [Link]
Knight helm - Plants vs Zombies 2 - PopCap Games (Juli 2013)
Assignments

 Assignments
 Available on Moodle
 [Link]
 lectures
 [Link]

 The only way to learn coding is to


sit down and code.
 Do the assignments, if you get stuck
send me an email
 This is not high school, so I won’t be
checking if you did them

You might also like