a beginner's guide to programming in...

123
A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R

Upload: others

Post on 03-Jun-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

A Beginner’s Guide to Programming in R

Drew Schmidt

June 9, 2015

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R

Page 2: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

About This Tutorial

Slides and Exercises

The slides and exercises are available at:http://wrathematics.info/handouts/dance2015.html

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R

Page 3: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Following Along

Log in to arronax.nics.utk.edu (you may wish to forward X11)

Run module load r/3.0.1

Run R

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R

Page 4: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Introduction to R

1 Introduction to RWhat is R?Resources and Advice

2 Basics

3 Programming

4 Project

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R

Page 5: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Introduction to R What is R?

1 Introduction to RWhat is R?Resources and Advice

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R

Page 6: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Introduction to R What is R?

What is R?

lingua franca for data analytics and statistical computing.

Part programming language, part data analysis package.

Dialect of S (Bell Labs).

Syntax designed for data.

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 4/105

Page 7: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Introduction to R What is R?

Who uses R?

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 5/105

Page 8: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Introduction to R What is R?

Language Paradigms

Functional

OO Dynamic Java

R Fortran 2003

Haskell

BASH

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 6/105

Page 9: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Introduction to R What is R?

The R Language

R is slow; if you don’t know what you’re doing, it’s really slow.

High-level scripting language.

Syntax designed for data: models are first-class constructs,missingness is built into the core of the language, . . .

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 7/105

Page 10: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Introduction to R What is R?

But you can’t deny its popularity!

IEEE Spectrum’s 2014 Ranking of Programming Languages

See:

http://spectrum.ieee.org/static/interactive-the-top-programming-languages#index

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 8/105

Page 11: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Introduction to R What is R?

At Build 2015 Microsoft CVP Joseph Sirosh called R the ”language ofdata” and said ”if there is a single language that you choose to learn

today .. let it be R”.

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 9/105

Page 12: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Introduction to R Resources and Advice

1 Introduction to RWhat is R?Resources and Advice

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R

Page 13: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Introduction to R Resources and Advice

R Resources

The Art of R Programming by Norm Matloff:http://nostarch.com/artofr.htm

An Introduction to R by Venables, Smith, and the R Core Team:http://cran.r-project.org/doc/manuals/R-intro.pdf

The R Inferno by Patrick Burns:http://www.burns-stat.com/pages/Tutor/R_inferno.pdf

Mathesaurus: http://mathesaurus.sourceforge.net/

R programming for those coming from other languages: http:

//www.johndcook.com/R_language_for_programmers.html

aRrgh: a newcomer’s (angry) guide to R, by Tim Smith and KevinUshey: http://tim-smith.us/arrgh/

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 10/105

Page 14: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Introduction to R Resources and Advice

Tutorials

R Programming, Coursera course through Johns Hopkinshttps://www.coursera.org/course/rprog

Statistics One Coursera course through Princetonhttps://www.coursera.org/course/stats1

High Performance Computing with R https://github.com/

wrathematics/2015hpcRworkshop/blob/master/README.md

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 11/105

Page 15: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Introduction to R Resources and Advice

Other Invaluable Resources

R Installation and Administration:http://cran.r-project.org/doc/manuals/R-admin.html

Task Views: http://cran.at.r-project.org/web/views

Writing R Extensions:http://cran.r-project.org/doc/manuals/R-exts.html

Mailing list archives: http://tolstoy.newcastle.edu.au/R/

The [R] stackoverflow tag.

The #rstats hastag on Twitter.

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 12/105

Page 16: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Introduction to R Resources and Advice

Comments and Advice

R is part statistics package, part programming language.

There are always 100 ways to do anything, only one of them efficient.

R is slow; if you don’t know what you’re doing, it’s really slow.

There is an R help mailing list. Use stackoverflow instead. . . .

Learn to love the R help system.

If something appears broken in core R, it’s probably not a bug (it’syou).

Just because something is on the CRAN does not mean it’s of anyvalue (or even functional).

Indent your code.

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 13/105

Page 17: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Introduction to R Resources and Advice

Comments and Advice

Be consistent.

Generally try to use instructive names for things.

Make your code concise, but not obtuse (don’t play golf).

Try to avoid “super functions”. Breaking up complicated ideas intomodular pieces can help with readability, debugging, reusability, etc.

Google has an R style guide: https://google-styleguide.

googlecode.com/svn/trunk/google-r-style.html

The R community has few computer scientists.

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 14/105

Page 18: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics

1 Introduction to R

2 BasicsR BasicsStringsI/OPlottingPackages

3 Programming

4 Project

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R

Page 19: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics R Basics

2 BasicsR BasicsStringsI/OPlottingPackages

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R

Page 20: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics R Basics

Starting R

1 Interactive: R

2 Batch: Rscript

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 15/105

Page 21: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics R Basics

Interacting with the Terminal

The default method in R is show(), which usually amounts to printing:

1 1

2 1+1

3 print (1+1)

4 sum

5

6 + # ctrl+c to break

7 "+"

8 ‘+‘

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 16/105

Page 22: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics R Basics

Arithmetic

1 7+4

2 7-4

3 7*4

4 7/4

5 7^4

6 7%%4

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 17/105

Page 23: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics R Basics

Assignment

R naming rules can be quite lax. For all practical purposes:

Start with a letter

Should consist of letters (any case), numbers, .’s, and ’s

Very strange things are possible, however. . .

1 "&*()" <- 3

2 "&*()"

3 "2" <- 1

4 ‘2‘ + 1

5 " _ " <- "even unicode"

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 18/105

Page 24: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics R Basics

Assignment

1 myvar <- 1

2 myvar

3

4 myvar <<- 2

5 myvar

6

7 assign(x="myvar", value =3)

8 myvar

9

10 myvar = 4

11 myvar

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 19/105

Page 25: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics R Basics

Case and Spacing

R is case sensitive, but fairly lax about spacing:

1 x <- 1:5

2 x

3 X

4

5 1 + 1

6 sum(x)

7 sum ( x )

8 s um(x)

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 20/105

Page 26: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics R Basics

Finding Help

R has its own manual system.

Most of the answers to your questions lie within.

Find help using ? or help(), or search across all help with ??.

1 ?sum

2 ??sum

3 help("sum")

4

5 ?+ # ctrl+c to break

6 ?"+"

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 21/105

Page 27: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics R Basics

RNG

R contains many powerful random number generators:

Beta Binomial CauchyChi-square Exponential FGamma Geometric HypergeometricLogistic Log Normal Negative BinomialNormal Poisson Student tUniform Weibull Wilcoxon Rank Sum

1 runif(5, min=0, max =10)

2 rnorm(5, mean=0, sd=1)

3 rgamma(5, shape =1)

4

5 set.seed (10)

6 runif (5)

7 set.seed (10)

8 runif (5)

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 22/105

Page 28: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics Strings

2 BasicsR BasicsStringsI/OPlottingPackages

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R

Page 29: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics Strings

Strings

“Character” data (text).

Internal storage scheme is complicated. . .

Mostly works like most high-level languages.

Can have a vector, matrix, dataframe, or list of strings.

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 23/105

Page 30: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics Strings

Strings: Example 1

1 letters

2 toupper(letters)

3 LETTERS

4 tolower(LETTERS)

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 24/105

Page 31: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics Strings

Strings: Example 2

1 x <- "Star Trek is objectively better than Star Wars"

2 strsplit(x, split=" ")

3

4 y <- unlist(strsplit(x, split=""))

5 y

6 paste(rev(y), collapse="")

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 25/105

Page 32: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics Strings

Strings: Example 3

1 paste(letters , letters)

2 paste(letters , letters , sep="")

3 paste(letters , letters , sep="", collapse="")

4

5 paste(paste(letters , collapse=""), paste(LETTERS , collapse=""),

sep="")

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 26/105

Page 33: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics Strings

Strings: Example 4

1 x <- rnorm (1000, mean =10)

2

3 paste("The mean of ’x’ is:", mean(x))

4

5 cat(sprintf("mean:\t%.2f\nvar:\t%.2f\n", mean(x), var(x)))

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 27/105

Page 34: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics Strings

Regular Expressions

grep(), grepl()

sub(), gsub()

Some others. . .

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 28/105

Page 35: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics I/O

2 BasicsR BasicsStringsI/OPlottingPackages

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R

Page 36: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics I/O

I/O

write.csv(), read.csv(),

write.table(), read.table()

scan()

save() and load()

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 29/105

Page 37: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics I/O

Reading and Writing a CSV

1 x <- matrix (1:30, 10)

2 write.csv(x, file="x.csv")

3 read.csv("x.csv")

4

5 write.csv(x, file="x.csv", row.names=F)

6 read.csv("x.csv", header=T)

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 30/105

Page 38: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics I/O

Serializing

1 x <- letters

2 y <- 1:5

3 z <- list(list("a"), b=matrix (0))

4

5 save(x, y, z, file="robjects.RData")

6 rm(x)

7 rm(y)

8 rm(z)

9 x

10 load("robjects.RData")

See also saveRDS().

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 31/105

Page 39: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics Plotting

2 BasicsR BasicsStringsI/OPlottingPackages

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R

Page 40: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics Plotting

Plotting

R is world class for graphics and visualization.

Biggest package for plotting is ggplot2 by Hadley Wickham.

We will focus on some examples from core R. . .

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 32/105

Page 41: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics Plotting

Scatterplots

1 x <- 1:10

2 y <- rnorm (10)

3 plot(x, y)

4 text(x=5.5, y=0, "MY PLOT", col=’red’)

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 33/105

Page 42: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics Plotting

Piecharts

1 pie (1:5)

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 34/105

Page 43: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics Plotting

Barplots

1 barplot (1:5)

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 35/105

Page 44: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics Plotting

Histograms

1 hist(rnorm (500))

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 36/105

Page 45: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics Plotting

Saving Plots

1 pdf("myscatterplot.pdf")

2 plot(rnorm (4), 1:4)

3 dev.off()

4

5 png("mybarplot.png")

6 barplot (1:10)

7 dev.off()

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 37/105

Page 46: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics Plotting

Plotting

These are all fairly “cookie-cutter”.

These can do more complicated things, but you’re only making thingsdifficult for yourself. . .

Best to learn a full graphics package.

The biggest are ggplot2 and lattice (I recommend ggplot. . . )

Lattice: http://www.statmethods.net/advgraphs/trellis.html

ggplot2: http://www.statmethods.net/advgraphs/ggplot2.html

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 38/105

Page 47: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics Packages

2 BasicsR BasicsStringsI/OPlottingPackages

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R

Page 48: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics Packages

Packages

Comprehensive R Archive Network (CRAN).

6735 packages.

CRAN Task Views http://cran.r-project.org/web/views/

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 39/105

Page 49: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics Packages

Installing Packages

From CRAN

1 install.packages("devtools")

From GitHub

1 library("devtools")

2 install_github("wrathematics/lineSampler")

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 40/105

Page 50: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics Packages

Namespaces

All packages have namespaces:

1 devtools :: install_github("wrathematics/lineSampler")

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 41/105

Page 51: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Basics Packages

Libraries

A package is a collection of code usable by R.

A library is a collection of packages.

library() loads a package.

Don’t think too much about this. . .

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 42/105

Page 52: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming

1 Introduction to R

2 Basics

3 ProgrammingType and Structures

TypeStructures

Control FlowLogicLoops*ply

FunctionsDebugging

Debugging R Code

The R DebuggerDebugging Compiled Code Called by R Code

4 Projectwrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R

Page 53: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Type and Structures

3 ProgrammingType and Structures

TypeStructures

Control FlowLogicLoops*ply

FunctionsDebugging

Debugging R Code

The R DebuggerDebugging Compiled Code Called by R Code

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R

Page 54: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Type and Structures

The R Language

Storage: logical, int, double, double complex, character (strings)

Structures: vector, matrix, array, list, dataframe

Caveats: (Logical) TRUE, FALSE, NA

In fact, there’s an NA for each type:

Integer: −(2−31 − 1

)Double: value at address 0x7FF00000000007A2LL

3 official OOP systems (none of them work like anything you’re usedto), several unofficial ones.

No official support for C++; several unofficial packages supportingC++.

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 43/105

Page 55: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Type and Structures

Data Types

logical

int

double

double complex

character

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 44/105

Page 56: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Type and Structures

Data Types

R is dynamically typed. You do not have to declare what kind of data avariable is before you start using it:

1 x <- 1

2 typeof(x)

3 x <- 1:2

4 typeof(x)

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 45/105

Page 57: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Type and Structures

Other

There are 4 “other” data “types”:

Inf

NaN

NULL

NA

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 46/105

Page 58: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Type and Structures

Inf

Numerical infinity

1 Inf

2 typeof(Inf)

3 is.finite(Inf)

4 is.infinite(Inf)

5 Inf+Inf

6 1/0

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 47/105

Page 59: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Type and Structures

NaN

Not a Number; numerical undefinedness.

1 NaN

2 typeof(NaN)

3 is.nan(NaN)

4 Inf -Inf

5 sin(Inf)

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 48/105

Page 60: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Type and Structures

NULL

The null object; a sort of placeholder for something undefined. Like anon-numeric NaN.

1 NULL

2 typeof(NULL)

3 is.null(NULL)

4 NULL+NULL

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 49/105

Page 61: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Type and Structures

NA

Missingness; not merely undefined, but unknown.

Each type (logical, int, double, . . . ) has its own NA

R is thus not boolean: TRUE, FALSE, NA

Most R methods have ways of removing NA’s

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 50/105

Page 62: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Type and Structures

Data Structures

vector

matrix

array

factor

list

dataframe

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 51/105

Page 63: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Type and Structures

Vectors

1 1:10

2 10:1

3

4 c(1, 3, 5, 7, 9)

5 seq(from=1, to=10, by=2)

6

7 x <- 1:5

8 length(x)

9 is.vector(x)

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 52/105

Page 64: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Type and Structures

Matrices

1 matrix (1:10)

2 matrix (1:10, nrow =5)

3 matrix (1:10, ncol =5)

4

5 x <- 1:10

6 y <- as.matrix(x)

7 dim(x)

8 dim(y)

9 dim(x) <- c(1, 10)

10 x

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 53/105

Page 65: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Type and Structures

Extraction

1 x <- matrix (1:30, 10)

2 x

3

4 x[-1, ]

5 x[, -1]

6 x[1:5, -1]

7 x[c(2,5,7), c(1,3)]

8

9 y <- x[, -2]

10 dim(y) <- NULL

11 y

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 54/105

Page 66: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Type and Structures

Replacement

1 x <- matrix (1:30, 10)

2 x

3

4 x[1:5, ] <- 0

5 x[7, 3] <- NA

6 x

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 55/105

Page 67: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Type and Structures

Factors

1 factor (1:5)

2 factor(c("a", "b", "b", "a", "c"))

3

4 x <- factor (-1:1)

5 x

6 as.numeric(x)

7 as.numeric(as.character(factor ( -1:1)))

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 56/105

Page 68: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Type and Structures

Dataframes

1 c(1, "a")

2 matrix(c(1, "a"))

3

4 x <- data.frame(1, "a")

5 x

6 x[1, 1]

7 is.numeric(x[1,1])

8 x[1, 2]

9 is.numeric(x[1,2])

10

11 data.frame(a=1:5,b=5:1)

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 57/105

Page 69: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Type and Structures

Lists

Super structures

Items can be any structure (even other lists)

Dataframe is really just a special list

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 58/105

Page 70: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Type and Structures

Lists

1 list (1)

2 x <- list(list (1), "a")

3 x

4 x[[1]]

5

6 x <- list(a=list("b", 1), z=1:5)

7 x$z

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 59/105

Page 71: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Type and Structures

Other Structures?

stacks, heaps, queues, graphs, . . .

tl;dr: no

Example deque https://github.com/wrathematics/dequer

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 60/105

Page 72: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Control Flow

3 ProgrammingType and Structures

TypeStructures

Control FlowLogicLoops*ply

FunctionsDebugging

Debugging R Code

The R DebuggerDebugging Compiled Code Called by R Code

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R

Page 73: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Control Flow

Logic

Possible values are TRUE, FALSE, and NA

Relational operations: ==, !=, <, <=, >, and >=

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 61/105

Page 74: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Control Flow

Logic

1 1==1

2 1!=1

3 1<1

4 1<=1

5

6 TRUE== FALSE

7

8 TRUE ==1

9 FALSE ==0

10

11 TRUE==T

12 FALSE==F

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 62/105

Page 75: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Control Flow

Logic

1 NA==NA

2 is.na(NA)

3

4 NULL==NULL

5 is.null(NULL)

6

7 NaN==NaN

8 is.nan(NaN)

9

10 Inf==Inf

11 is.infinite(Inf)

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 63/105

Page 76: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Control Flow

Logic

Vectors of logicals are evaluated element-wise:

1 x <- c(T, F, F, T, T, F)

2 x==T

3 !x

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 64/105

Page 77: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Control Flow

Logic

Some very important functions for logical evaluations:

1 x <- c(T, F, F, T, T, F)

2 any(x)

3 all(x)

4 which(x)

5

6 y <- -5:5

7 which(y%%2==0)

8 y[which(y%% 2==0)]

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 65/105

Page 78: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Control Flow

Loops

Instruction set to be repeatedly executed until some condition issatisfied (possibly forever).

R has for() and while().

for(): Iterates over a list or vector

while(): Performs operations until logical condition is satisfied.

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 66/105

Page 79: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Control Flow

for Loop

1 for (i in 1:10){

2 print(i)

3 }

4

5 x <- matrix (1:30, 10)

6 colmax <- numeric (3)

7 for (i in 1:3){

8 colmax[i] <- max(x[, i])

9 }

10 colmax

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 67/105

Page 80: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Control Flow

while Loop

1 i <- 1

2 while (i < 11){

3 print(i)

4 i <- i+1

5 }

6

7 n <- 2

8 i <- 1

9 while (n < 1000){

10 n <- n^2

11 i <- i*2

12 }

13 n

14 i

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 68/105

Page 81: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Control Flow

The *ply Family

apply(): Apply function across “margin” (dimension) of matrix.

lapply(): Apply function to input data object; returns a list.

sapply(): Same as sapply() but

mapply(): Multivariate sapply().

vapply(): Essentially sapply(), sometimes faster.

tapply(): Applying a function to a subset of a vector.

We will only discuss the first 3.

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 69/105

Page 82: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Control Flow

apply

1 x <- matrix (1:30, 10)

2 x

3 apply(X=x, MARGIN=1, FUN=min)

4 apply(X=x, MARGIN=2, FUN=min)

5

6 out <- numeric (3)

7 for (i in 1:3){

8 out[i] <- min(x[, i])

9 }

10 out

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 70/105

Page 83: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Control Flow

lapply and sapply

1 x <- 1:5

2 lapply(X=x, FUN=sqrt)

3

4 sapply(X=x, FUN=sqrt)

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 71/105

Page 84: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Control Flow

*ply Functions Internally

apply(): A for() loop.

lapply(): Internal R voodoo; faster than a loop.

sapply(): Essentially the same as lapply().

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 72/105

Page 85: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Control Flow

When does all this choice matter?

loops are slow.

apply() is sugar for an R for loop.

lapply() different, often faster.

Vectorization is fastest of all.

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 73/105

Page 86: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Control Flow

Loop Speeds

1 # No initialization

2 system.time({

3 x <- 5000:1

4 sin <- numeric (0)

5 for (i in 1: length(x)){

6 sin[i] <- sin(x[i])

7 }

8 sin

9 })

10

11 # With initialization

12 system.time({

13 x <- 5000:1

14 sin <- numeric(length(x))

15 for (i in 1: length(x)){

16 sin[i] <- sin(x[i])

17 }

18 sin

19 })

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 74/105

Page 87: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Control Flow

Loop Speeds

1 # lapply

2 system.time(lapply(x, sin))

3 system.time(sapply(x, sin))

4

5 # vectorized

6 system.time(sin(x))

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 75/105

Page 88: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Functions

3 ProgrammingType and Structures

TypeStructures

Control FlowLogicLoops*ply

FunctionsDebugging

Debugging R Code

The R DebuggerDebugging Compiled Code Called by R Code

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R

Page 89: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Functions

Functions

Self-contained input/output machine.

Reusable blocks of code.

First class objects.

Can not modify in place∗; use a list.

Evaluating the Design of the R language,http://r.cs.purdue.edu/pub/ecoop12.pdf

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 76/105

Page 90: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Functions

Functions: Example 1

1 f <- function(x)

2 {

3 ret <- x+1

4 return(ret)

5 }

6

7 f(1)

8 f(2)

9 f(5)

10 f

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 77/105

Page 91: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Functions

Functions: Example 2

1 f <- function (a, b)

2 {

3 a - b

4 }

5

6 f(a =1, b=2)

7 f(1, 2)

8 f(b=1, a=2)

9 f(b=1, 2)

10 f(1)

11 f(matrix (1:4, ncol =2), matrix (4:1, nrow =2))

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 78/105

Page 92: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Functions

Functions: Example 3

For complicated returns (especially of mixed type/class), use a list:

1 g <- function (a, b)

2 {

3 return(list(plus=a+b, minus=a-b, times=a*b, division=a/b))

4 }

5

6 g(5, 2)

7 g(1, 0)

8 g(f(2, 6), 2)

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 79/105

Page 93: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Functions

Functions: Example 4

R allows parameter defaults in functions

1 h <- function (a=1, b=2)

2 {

3 return(b-a)

4 }

5

6 h

7 h()

8 h(2, 1)

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 80/105

Page 94: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Functions

Recursive Functions

A recursive function is one that calls itself:

1 f <- function(n)

2 {

3 if (n==1)

4 return (1)

5 else {

6 if (n%% 2==0)

7 return(f(n/2))

8 else

9 return(f(3*n+1))

10 }

11 }

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 81/105

Page 95: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Debugging

3 ProgrammingType and Structures

TypeStructures

Control FlowLogicLoops*ply

FunctionsDebugging

Debugging R Code

The R DebuggerDebugging Compiled Code Called by R Code

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R

Page 96: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Debugging

Debugging R Code

Very broad topic . . .

We’ll hit the highlights.

For more examples, see:cran.r-project.org/doc/manuals/R-exts.html#Debugging

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 82/105

Page 97: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Debugging

Object Inspection Tools

print()

str()

unclass()

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 83/105

Page 98: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Debugging

Object Inspection Tools: print()

Basic printing:

1 > x <- matrix (1:10 , nrow =2)

2 > print(x)

3 [,1] [,2] [,3] [,4] [,5]

4 [1,] 1 3 5 7 9

5 [2,] 2 4 6 8 10

6 > x

7 [,1] [,2] [,3] [,4] [,5]

8 [1,] 1 3 5 7 9

9 [2,] 2 4 6 8 10

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 84/105

Page 99: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Debugging

Object Inspection Tools: str()

Examining the structure of an R object:

1 > x <- matrix (1:10 , nrow =2)

2 > str(x)

3 int [1:2, 1:5] 1 2 3 4 5 6 7 8 9 10

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 85/105

Page 100: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming Debugging

Object Inspection Tools: unclass()

Exposing all data with unclass():

1 df <- data.frame(x=rnorm (10), y=rnorm (10))

2 mdl <- lm(y~x, data=df) ### That ’s a "tilde" character

3

4 mdl

5 print(mdl)

6

7 str(mdl)

8

9 unclass(mdl)

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 86/105

Page 101: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming The R Debugger

3 ProgrammingType and Structures

TypeStructures

Control FlowLogicLoops*ply

FunctionsDebugging

Debugging R Code

The R DebuggerDebugging Compiled Code Called by R Code

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R

Page 102: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming The R Debugger

The R Debugger

debug()

debugonce()

undebug()

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 87/105

Page 103: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming The R Debugger

Using The R Debugger

1 Declare function to be debugged: debug(foo)

2 Call function: foo(arg1, arg2, ...)

next: Enter or n followed by Enter.break: Halt execution and exit debugging: Q.

exit: Continue execution and exit debugging: c.

3 Call undebug() to stop debugging

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 88/105

Page 104: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming The R Debugger

Using the Debugger

Example Debugger Interaction

1 > f <- function(x){y <- z+1;z <- y*2;z}

2 > f(1)

3 Error in f(1) : object ’z’ not found

4 > debug(f)

5 > f(1)

6 debugging in: f(1)

7 debug at #1: {

8 y <- z + 1

9 z <- y * 2

10 z

11 }

12 Browse [2]>

13 debug at #1: y <- z + 1

14 Browse [2]>

15 Error in f(1) : object ’z’ not found

16 >

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 89/105

Page 105: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming The R Debugger

Debugging Compiled Code

Reasonably easy to use gdb andValgrind (from command line).

gdb — The GNU Debugger;general purpose debugging.

Valgrind — Memory debugger.

For gdb, start R interactively.

For Valgrind, need a batchscript.

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 90/105

Page 106: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming The R Debugger

Debugging with gdb

Suppose we have:

R function: fooR()

Calls the C function: fooC()

We can debug fooC() via gdb by executing the following from a shell:

1 R -d gdb

2 b fooC

3 signal 0

4 fooR (10)

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 91/105

Page 107: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Programming The R Debugger

Debugging with Valgrind

Put the R code you wish to profile in myscript.r and execute thefollowing from a shell:

1 R -d "valgrind --tool=memcheck --leak -check=full" --vanilla <

myscript.r

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 92/105

Page 108: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Project

1 Introduction to R

2 Basics

3 Programming

4 ProjectGetting StartedUsing xts

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R

Page 109: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Project Getting Started

4 ProjectGetting StartedUsing xts

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R

Page 110: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Project Getting Started

Getting Started

Log in to arronax.nics.utk.edu

Run module load r/3.0.1

Run cd /lustre/medusa/proj/dance/data/day 0129 csv

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 93/105

Page 111: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Project Getting Started

Managing the Data

ls

ls | grep Unit989

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 94/105

Page 112: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Project Getting Started

Managing the Data

reconfigure.sh

1 #!/bin/sh

2

3 for file in ‘ls | grep Unit989 ‘; do

4 cat $file >> Unit989.csv

5 done

1 $ chmod +x reconfigure.sh

2 $ ./reconfigure.sh

3 $ wc -l Unit989.csv

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 95/105

Page 113: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Project Getting Started

Managing the Data

reconfigure.sh

1 #!/bin/sh

2

3 for folder in ‘ls -d */ | grep -E

"day_[0 -9][0 -9][0 -9][0 -9]_csv" ‘; do

4 for file in ‘ls $folder | grep Unit989 ‘; do

5 cat $folder/$file >> Unit989.csv

6 done

7 done

1 $ chmod +x reconfigure.sh

2 $ ./reconfigure.sh

3 $ wc -l Unit989.csv

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 96/105

Page 114: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Project Getting Started

Loading

1 file <- "Unit989 -20150129 -000102. csv"

2 file.info(file)

3 data <- read.csv(file)

4 head(data)

5 str(data)

6 data <- read.csv(file , header=FALSE , stringsAsFactors=FALSE)

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 97/105

Page 115: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Project Getting Started

Inspecting

1 head(data)

2 tail(data)

3 dim(data)

4 str(data)

5 summary(data)

6 sd(data$V5)

7 object.size(data)

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 98/105

Page 116: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Project Using xts

4 ProjectGetting StartedUsing xts

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R

Page 117: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Project Using xts

xts: The eXtensible Time Series package

1 library(xts)

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 99/105

Page 118: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Project Using xts

Time Series

1 date <- as.POSIXlt(paste(data$V1 , data$V2))

2 x <- xts(data$V4, date)

3 head(x)

4 str(x)

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 100/105

Page 119: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Project Using xts

Time Series

1 first(x, "3 seconds")

2 first(x, "1 minute")

3 last(first(x, "1 minute"), "3 seconds")

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 101/105

Page 120: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Project Using xts

Time Series

1 ndays(x)

2 nhours(x)

3 nminutes(x)

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 102/105

Page 121: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Project Using xts

Time Series

1 to.daily(x)

2 to.hourly(x)

3 to.minutes30(x)

4 to.minutes15(x)

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 103/105

Page 122: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Project Using xts

Time Series

1 endpoints(x, "hours")

2 period.apply(x, INDEX=endpoints(x, "hours"), FUN=mean)

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 104/105

Page 123: A Beginner's Guide to Programming in Rlibrestats.com/_pages/files/presentations/2015/DAnCE.pdf · A Beginner’s Guide to Programming in R Drew Schmidt June 9, 2015 wrathematics.info/handouts/dance2015.html

Project Using xts

Time Series

1 min30 <- to.minutes30(x)

2

3 plot(x)

4 plot(min30)

5

6 plot(as.zoo(min30))

7 plot(as.zoo , plot.type="single")

8 plot(as.zoo , plot.type="single", col =1:4)

wrathematics.info/handouts/dance2015.html Drew Schmidt A Beginner’s Guide to Programming in R 105/105