Presentation 1: R Murray Logan 10 Apr 2016 Schedule - 1 week - - PowerPoint PPT Presentation
Presentation 1: R Murray Logan 10 Apr 2016 Schedule - 1 week - - PowerPoint PPT Presentation
Presentation 1: R Murray Logan 10 Apr 2016 Schedule - 1 week course Day AM PM Monday Intro to R Intro to hypothesis testing Tuesday Simple Multiple regression regression Wednesday One-way ANOVA Factorial ANOVA Thursday Mixed
Schedule - 1 week course
Day AM PM Monday Intro to R Intro to hypothesis testing Tuesday Simple regression Multiple regression Wednesday One-way ANOVA Factorial ANOVA Thursday Mixed effects models Frequency analysis Friday Generalized linear models Generalized linear models
Schedule - 2 week course
Day AM PM Monday Intro to R Intro to ggplot Tuesday Simple regression Multiple regression Wednesday One-way ANOVA Factorial ANOVA Thursday Mixed effects models Mixed effects models Friday Generalized linear models Generalized linear models Monday Generalized linear mixed effects models Generalized additive models Tuesday Bayesian linear models Bayesian linear models Wednesday Bayesian hierarchical models Bayesian hierarchical models Thursday Baysian generalized models Bayesian generalized models Friday Multivariate analysis Multivariate analysis
Course
www.flutterbys.com.au/stats
Opening thoughts
What is R and Why learn it?
- R is a open source graphical and
statistical environment
- command driven programming language
- clone of S
- 5459 packages on CRAN alone
Preparation
- 1. R and an R editor
- 2. knitr
R Editors
- Issue commands in a terminal
R Editors
- Issue commands in a terminal
- Windows and MacOSX versions come with GUI
R Editors
- Issue commands in a terminal
- Windows and MacOSX versions come with GUI
- A text editor for writing scripts
- RStudio
R studio
R studio
Reproducible research
- Packaging together
- code
- notes
- output and
- interpretations
- Single source
Reproducible research
- Packaging together
- code
- notes
- output and
- interpretations
- Single source
- Markdown language
- simple structure
- embed R code
- Variety of output (html, pdf, etc)
Reproducible research
- install knitr package
- Options --> Sweave --> Weave to knitr
- New --> R Markdown
Cheat sheets (ref cards)
https://www.rstudio.com/resources/cheatsheets/
AND NOW FOR R
Scripts and commands
- enter a command (expression) at the prompt
(>)
- store commands in a script
Basic syntax
The R Environment and command line
> 5+1 [1] 6
Basic syntax
The R Environment and command line
> 5+1 [1] 6 > VAR1 <- 2 + 3 > VAR1 [1] 5
Basic syntax
The R Environment and command line
> 5+1 [1] 6 > VAR1 <- 2 + 3 > VAR1 [1] 5 > VAR2 <- + 2+3 > VAR2 [1] 5
Basic syntax
The R Environment and command line
> 5+1 [1] 6 > VAR1 <- 2 + 3 > VAR1 [1] 5 > VAR2 <- + 2+3 > VAR2 [1] 5 > VAR2- 1 [1] 4 > ANS1 <- VAR1 * VAR2 > ANS1 [1] 25
Your turn
- using an inbuilt constant for π (pi),
calculate the area of a circle of radius 10cm
> pi [1] 3.141593
Your turn
- using an inbuilt constant for π (pi),
calculate the area of a circle of radius 10cm
> RADIUS <- 10 > AREA <- pi*RADIUS^2 > AREA [1] 314.1593 > #OR > pi*10^2 [1] 314.1593
Basic syntax
O b j e c t n a m e s
- must start with a letter
- cannot include a space or - + * / # % &
[ ] { } ( ) ~
Basic syntax
O b j e c t n a m e s
Good practice
- avoid lower case names (conflict)
- should reflect contents
- use underscore and periods to separate
words
> MEAN.HEAD.LENGTH <- 1
Basic syntax
C
- n
c a t e n a t i n g
- b
j e c t s
> c(1,2,6) [1] 1 2 6 > c(VAR1,VAR2) [1] 5 5
Basic syntax
C
- n
c a t e n a t i n g
- b
j e c t s
> c(1,2,6) [1] 1 2 6 > c(VAR1,VAR2) [1] 5 5
c() is a function
Basic syntax
C
- n
c a t e n a t i n g
- b
j e c t s
> c(1,2,6) [1] 1 2 6 > c(VAR1,VAR2) [1] 5 5
c() is a function
> RADIUS <- c(10,20,30) > AREA <- pi*RADIUS^2 > AREA [1] 314.1593 1256.6371 2827.4334
Comments
> c(1,2,6) # ignore all after the # sign [1] 1 2 6 > # allow you to annotate your code
Scripting Advice #1
- 1. Make your code as readable as possible
- use meaningful names
- make use of indentation and spaces
Scripting Advice #1
- 1. Make your code as readable as possible
- use meaningful names
- make use of indentation and spaces
- 2. Use comments to document what and why
Basic usage
C
- m
m a n d h i s t
- r
y
- up and down arrows
Basic usage
T h e w
- r
k s p a c e
> # list all objects created by the user in the session > ls() [1] "ANS1" "AREA" "fn" "MEAN.HEAD.LENGTH" "RADIUS" [6] "tab.count" "VAR1" "VAR2"
Basic usage
T h e w
- r
k s p a c e
> # list all objects created by the user in the session > ls() [1] "ANS1" "AREA" "fn" "MEAN.HEAD.LENGTH" "RADIUS" [6] "tab.count" "VAR1" "VAR2" > # list all objects created by the user in the session that match a specific pattern > ls(pat="VAR") [1] "VAR1" "VAR2" > ls(pat="A*1") [1] "ANS1" "VAR1"
Basic usage
T h e w
- r
k s p a c e
> rm(VAR1,VAR2) #remove the VAR1 and VAR2 objects > rm(list=ls()) #remove all user defined objects
Basic usage
T h e w
- r
k i n g d i r e c t
- r
y
> setwd("/home/murray/tmp/") #change the current working directory path > getwd() #review the current working directory [1] "/home/murray/tmp"
Basic usage
T h e w
- r
k i n g d i r e c t
- r
y
> setwd("/home/murray/tmp/") #change the current working directory path > getwd() #review the current working directory [1] "/home/murray/tmp"
Note that R uses Unix directory slashes (/) NOT Windows directory slashes (\)
> list.files(path=getwd()) #list all files (and directories) in the current working directory [1] "A_knit_.rmd~" "A.Rmd~" "beamerHeader.sty" [4] "cache" "courses.aux" "courses.html" [7] "courses.log" "courses.nav" "courses_notes.aux" [10] "courses_notes.log" "courses_notes.out" "courses_notes.pdf" [13] "courses_notes.tex" "courses_notes.toc" "courses.out" [16] "courses.pdf" "courses.R" "courses.Rmd" [19] "courses.Rmd~" "courses.snm" "courses.tex" [22] "courses_tmp.md" "courses.toc" "end1.matter" [25] "figure" "getInits.R" "ggmapTemp.png" [28] "head.template" "images" "junk2.dzslides" [31] "macnally.jpg" "Makefile" "Makefile~" [34] "MeansParam.aux" "MeansParam.log" "MeansParam.pdf" [37] "#model.txt#" "model.txt" "NotebookPaper.jpg" [40] "pres.10.4.aux" "pres.10.4.html" "pres.10.4.log" [43] "pres.10.4.nav" "pres.10.4_notes.aux" "pres.10.4_notes.log" [46] "pres.10.4_notes.out" "pres.10.4_notes.pdf" "pres.10.4_notes.tex" [49] "pres.10.4_notes.toc" "pres.10.4.out" "pres.10.4.pdf" [52] "pres.10.4.R" "pres.10.4.Rmd" "pres.10.4.Rmd~" [55] "pres.10.4.snm" "pres.10.4.tex" "pres.10.4_tmp.md" [58] "pres.10.4.toc" "pres.10.4.vrb" "pres.10.5a.aux" [61] "pres.10.5a.html" "pres.10.5a.log" "pres.10.5a.nav" [64] "pres.10.5a_notes.aux" "pres.10.5a_notes.log" "pres.10.5a_notes.out" [67] "pres.10.5a_notes.pdf" "pres.10.5a_notes.tex" "pres.10.5a_notes.toc" [70] "pres.10.5a.out" "pres.10.5a.pdf" "pres.10.5a.R" [73] "#pres.10.5a.Rmd#" "pres.10.5a.Rmd" "pres.10.5a.Rmd~" [76] "pres.10.5a.snm" "pres.10.5a.tex" "pres.10.5a_tmp.md" [79] "pres.10.5a.toc" "pres.10.5a.vrb" "pres.10.6a.aux" [82] "pres.10.6a.html" "pres.10.6a.log" "pres.10.6a.nav" [85] "pres.10.6a_notes.aux" "pres.10.6a_notes.log" "pres.10.6a_notes.out" [88] "pres.10.6a_notes.pdf" "pres.10.6a_notes.tex" "pres.10.6a_notes.toc" [91] "pres.10.6a.out" "pres.10.6a.pdf" "pres.10.6a.R" [94] "#pres.10.6a.Rmd#" "pres.10.6a.Rmd" "pres.10.6a.Rmd~" [97] "pres.10.6a.snm" "pres.10.6a.tex" "pres.10.6a_tmp.md" [100] "pres.10.6a.toc" "pres.10.6a.vrb" "pres.11.2a.aux" [103] "pres.11.2a.html" "pres.11.2a.log" "pres.11.2a.md" [106] "pres.11.2a.nav" "pres.11.2a_notes.aux" "pres.11.2a_notes.log" [109] "pres.11.2a_notes.out" "pres.11.2a_notes.pdf" "pres.11.2a_notes.tex" [112] "pres.11.2a_notes.toc" "pres.11.2a.out" "pres.11.2a.pdf" [115] "pres.11.2a.R" "#pres.11.2a.Rmd#" "pres.11.2a.Rmd" [118] "pres.11.2a.Rmd~" "pres.11.2a.snm" "pres.11.2a.tex" [121] "pres.11.2a_tmp.md" "pres.11.2a.toc" "pres.11.2a.vrb" [124] "pres.12.2.aux" "pres.12.2.html" "pres.12.2.log" [127] "pres.12.2.md" "pres.12.2.nav" "pres.12.2_notes.aux" [130] "pres.12.2_notes.log" "pres.12.2_notes.out" "pres.12.2_notes.pdf" [133] "pres.12.2_notes.tex" "pres.12.2_notes.toc" "pres.12.2.out" [136] "pres.12.2.pdf" "pres.12.2.R" "#pres.12.2.Rmd#" [139] "pres.12.2.Rmd" "pres.12.2.snm" "pres.12.2.tex" [142] "pres.12.2_tmp.md" "pres.12.2.toc" "pres.12.2.vrb" [145] "pres.13.aux" "pres.13.html" "pres.13.log" [148] "pres.13.nav" "pres.13_notes.aux" "pres.13_notes.log" [151] "pres.13_notes.out" "pres.13_notes.pdf" "pres.13_notes.tex" [154] "pres.13_notes.toc" "pres.13.out" "pres.13.pdf" [157] "pres.13.R" "pres.13.Rmd" "pres.13.Rmd~" [160] "pres.13.snm" "pres.13.tex" "pres.13_tmp.md" [163] "pres.13.toc" "pres.13.vrb" "pres.14.aux" [166] "pres.14.html" "pres.14.log" "pres.14.md" [169] "pres.14.nav" "pres.14_notes.aux" "pres.14_notes.log" [172] "pres.14_notes.out" "pres.14_notes.pdf" "pres.14_notes.tex" [175] "pres.14_notes.toc" "pres.14.out" "pres.14.pdf" [178] "pres.14.R" "pres.14.Rmd" "pres.14.Rmd~" [181] "pres.14.snm" "pres.14.tex" "pres.14_tmp.md" [184] "pres.14.toc" "pres.14.vrb" "pres.15.aux" [187] "pres.15.html" "pres.15.log" "pres.15.nav" [190] "pres.15_notes.aux" "pres.15_notes.log" "pres.15_notes.out" [193] "pres.15_notes.pdf" "pres.15_notes.tex" "pres.15_notes.toc" [196] "pres.15.out" "pres.15.pdf" "pres.15.R" [199] "#pres.15.Rmd#" "pres.15.Rmd" "pres.15.Rmd~" [202] "pres.15.snm" "pres.15.tex" "pres.15_tmp.md" [205] "pres.15.toc" "pres.15.vrb" "pres.1.aux" [208] "pres.1.html" "pres.1.log" "pres.1.md" [211] "pres.1.nav" "pres.1_notes.aux" "pres.1_notes.log" [214] "pres.1_notes.out" "pres.1_notes.pdf" "pres.1_notes.tex" [217] "pres.1_notes.toc" "pres.1old.aux" "pres.1old.html" [220] "pres.1old_knit_.Rmd" "pres.1old.log" "pres.1old.nav" [223] "pres.1old_notes.aux" "pres.1old_notes.log" "pres.1old_notes.out" [226] "pres.1old_notes.pdf" "pres.1old_notes.tex" "pres.1old_notes.toc" [229] "pres.1old.out" "pres.1old.pdf" "pres.1old.R" [232] "pres.1old.Rmd" "pres.1old.Rmd~" "pres.1old.snm" [235] "pres.1old.tex" "pres.1old_tmp.md" "pres.1old.toc" [238] "pres.1old.vrb" "pres.1.out" "pres.1.pdf" [241] "pres.1.R" "pres.1.Rmd" "pres.1.Rmd~" [244] "pres.1.snm" "pres.1.tex" "pres.1_tmp.md" [247] "pres.1.toc" "pres.1.vrb" "pres.2.1.aux" [250] "pres.2.1.html" "pres.2.1.log" "pres.2.1.nav" [253] "pres.2.1_notes.aux" "pres.2.1_notes.log" "pres.2.1_notes.out" [256] "pres.2.1_notes.pdf" "pres.2.1_notes.tex" "pres.2.1_notes.toc" [259] "pres.2.1.out" "pres.2.1.pdf" "pres.2.1.R" [262] "pres.2.1.Rmd" "pres.2.1.Rmd~" "pres.2.1.snm" [265] "pres.2.1.tex" "pres.2.1_tmp.md" "pres.2.1.toc" [268] "pres.2.1.vrb" "pres.2.4.aux" "pres.2.4.html" [271] "pres.2.4.log" "pres.2.4.md" "pres.2.4.nav" [274] "pres.2.4_notes.aux" "pres.2.4_notes.log" "pres.2.4_notes.out" [277] "pres.2.4_notes.pdf" "pres.2.4_notes.tex" "pres.2.4_notes.toc" [280] "pres.2.4old.aux" "pres.2.4old.html" "pres.2.4old.log" [283] "pres.2.4old.nav" "pres.2.4old_notes.aux" "pres.2.4old_notes.log" [286] "pres.2.4old_notes.out" "pres.2.4old_notes.pdf" "pres.2.4old_notes.tex" [289] "pres.2.4old_notes.toc" "pres.2.4old.out" "pres.2.4old.pdf" [292] "pres.2.4old.R" "pres.2.4old.Rmd" "pres.2.4old.snm" [295] "pres.2.4old.tex" "pres.2.4old_tmp.md" "pres.2.4old.toc" [298] "pres.2.4old.vrb" "pres.2.4.out" "pres.2.4.pdf" [301] "pres.2.4.R" "pres.2.4.Rmd" "pres.2.4.Rmd~" [304] "pres.2.4.snm" "pres.2.4.tex" "pres.2.4_tmp.md" [307] "pres.2.4.toc" "pres.2.4.vrb" "pres.3.2.html" [310] "pres.3.2.pdf" "pres.3.2.R" "pres.3.2.Rmd" [313] "pres.3.2.Rmd~" "pres.4.html" "pres.4.html~" [316] "pres.4_notes.pdf" "pres.4_notes.tex~" "pres.4.pdf" [319] "pres.4pdf_knit_.Rmd~" "pres.4.R" "pres.4.Rmd" [322] "pres.4.Rmd~" "pres.4.tex~" "pres.4_tmp.md~" [325] "pres.5.2.aux" "pres.5.2.html" "pres.5.2.log" [328] "pres.5.2.md" "pres.5.2.nav" "pres.5.2_notes.aux" [331] "pres.5.2_notes.log" "pres.5.2_notes.out" "pres.5.2_notes.pdf" [334] "pres.5.2_notes.tex" "pres.5.2_notes.toc" "pres.5.2.out" [337] "pres.5.2.pdf" "pres.5.2.R" "#pres.5.2.Rmd#" [340] "pres.5.2.Rmd" "pres.5.2.Rmd~" "pres.5.2.snm" [343] "pres.5.2.tex" "pres.5.2.tex~" "pres.5.2_tmp.md" [346] "pres.5.2.toc" "pres.5.2.vrb" "pres.7.2a.aux" [349] "pres.7.2a.html" "pres.7.2a.log" "pres.7.2a.nav" [352] "pres.7.2a_notes.aux" "pres.7.2a_notes.log" "pres.7.2a_notes.out" [355] "pres.7.2a_notes.pdf" "pres.7.2a_notes.tex" "pres.7.2a_notes.toc" [358] "pres.7.2a.out" "pres.7.2a.pdf" "pres.7.2a.R" [361] "#pres.7.2a.Rmd#" "pres.7.2a.Rmd" "pres.7.2a.Rmd~" [364] "pres.7.2a.snm" "pres.7.2a.tex" "pres.7.2a.tex~" [367] "pres.7.2a_tmp.md" "pres.7.2a.toc" "pres.7.2a.vrb" [370] "pres.7.3a.aux" "pres.7.3a.html" "pres.7.3a.log" [373] "pres.7.3a.nav" "pres.7.3a_notes.aux" "pres.7.3a_notes.log" [376] "pres.7.3a_notes.out" "pres.7.3a_notes.pdf" "pres.7.3a_notes.tex" [379] "pres.7.3a_notes.toc" "pres.7.3a.out" "pres.7.3a.pdf" [382] "pres.7.3a.R" "#pres.7.3a.Rmd#" "pres.7.3a.Rmd" [385] "pres.7.3a.Rmd~" "pres.7.3a.snm" "pres.7.3a.tex" [388] "pres.7.3a_tmp.md" "pres.7.3a.toc" "pres.7.3a.vrb" [391] "pres.7.4a.aux" "pres.7.4a.html" "pres.7.4a.log" [394] "pres.7.4a.nav" "pres.7.4a_notes.aux" "pres.7.4a_notes.log" [397] "pres.7.4a_notes.out" "pres.7.4a_notes.pdf" "#pres.7.4a_notes.tex#" [400] "pres.7.4a_notes.tex" "pres.7.4a_notes.tex~" "pres.7.4a_notes.toc" [403] "pres.7.4a.out" "pres.7.4a.pdf" "pres.7.4a.R" [406] "pres.7.4a.Rmd" "pres.7.4a.Rmd~" "pres.7.4a.snm" [409] "pres.7.4a.tex" "pres.7.4a.tex~" "pres.7.4a_tmp.md" [412] "pres.7.4a.toc" "pres.7.4a.vrb" "pres.7.5a.aux" [415] "pres.7.5a.html" "pres.7.5a.log" "pres.7.5a.nav" [418] "pres.7.5a_notes.aux" "pres.7.5a_notes.log" "pres.7.5a_notes.out" [421] "pres.7.5a_notes.pdf" "pres.7.5a_notes.tex" "pres.7.5a_notes.toc" [424] "pres.7.5a.out" "pres.7.5a.pdf" "pres.7.5a.R" [427] "#pres.7.5a.Rmd#" "pres.7.5a.Rmd" "pres.7.5a.Rmd~" [430] "pres.7.5a.snm" "pres.7.5a.tex" "pres.7.5a_tmp.md" [433] "pres.7.5a.toc" "pres.7.5a.vrb" "pres.7.6a.aux" [436] "pres.7.6a.html" "pres.7.6a.log" "pres.7.6a.nav" [439] "pres.7.6a_notes.aux" "pres.7.6a_notes.log" "pres.7.6a_notes.out" [442] "pres.7.6a_notes.pdf" "pres.7.6a_notes.tex" "pres.7.6a_notes.toc" [445] "pres.7.6a.out" "pres.7.6a.pdf" "pres.7.6a.R" [448] "pres.7.6a.Rmd" "pres.7.6a.Rmd~" "pres.7.6a.snm" [451] "pres.7.6a.tex" "pres.7.6a_tmp.md" "pres.7.6a.toc" [454] "pres.7.6a.vrb" "pres.8.2a.aux" "pres.8.2a.html" [457] "pres.8.2a.log" "pres.8.2a.md" "pres.8.2a.nav" [460] "pres.8.2a_notes.aux" "pres.8.2a_notes.log" "pres.8.2a_notes.out" [463] "pres.8.2a_notes.pdf" "pres.8.2a_notes.tex" "pres.8.2a_notes.tex~" [466] "pres.8.2a_notes.toc" "pres.8.2a.out" "pres.8.2a.pdf" [469] "pres.8.2a.R" "#pres.8.2a.Rmd#" "pres.8.2a.Rmd" [472] "pres.8.2a.Rmd~" "pres.8.2a.snm" "pres.8.2a.tex" [475] "pres.8.2a_tmp.md" "pres.8.2a.toc" "pres.8.2a.vrb" [478] "pres.8.3a.aux" "pres.8.3a.html" "pres.8.3a.log" [481] "pres.8.3a.md" "pres.8.3a.nav" "pres.8.3a_notes.aux" [484] "pres.8.3a_notes.log" "pres.8.3a_notes.out" "pres.8.3a_notes.pdf" [487] "pres.8.3a_notes.tex" "pres.8.3a_notes.toc" "pres.8.3a.out" [490] "pres.8.3a.pdf" "pres.8.3a.R" "#pres.8.3a.Rmd#" [493] "pres.8.3a.Rmd" "pres.8.3a.Rmd~" "pres.8.3a.snm" [496] "pres.8.3a.tex" "pres.8.3a_tmp.md" "pres.8.3a.toc" [499] "pres.8.3a.vrb" "pres.9.1a.Rmd~" "pres.9.1.aux" [502] "pres.9.1.html" "pres.9.1.log" "pres.9.1.md" [505] "pres.9.1.nav" "pres.9.1_notes.aux" "pres.9.1_notes.log" [508] "pres.9.1_notes.out" "pres.9.1_notes.pdf" "pres.9.1_notes.tex" [511] "pres.9.1_notes.toc" "pres.9.1.out" "pres.9.1.pdf" [514] "pres.9.1.R" "pres.9.1.Rmd" "pres.9.1.Rmd~" [517] "pres.9.1.snm" "pres.9.1.tex" "pres.9.1_tmp.md" [520] "pres.9.1.toc" "pres.9.1.vrb" "pres.9.2a.aux" [523] "pres.9.2a.html" "pres.9.2a.log" "pres.9.2a.nav" [526] "pres.9.2a_notes.aux" "pres.9.2a_notes.log" "pres.9.2a_notes.out" [529] "pres.9.2a_notes.pdf" "pres.9.2a_notes.tex" "pres.9.2a_notes.toc" [532] "pres.9.2a.out" "pres.9.2a.pdf" "pres.9.2a.R" [535] "pres.9.2a.Rmd" "pres.9.2a.Rmd~" "pres.9.2a.snm" [538] "pres.9.2a.tex" "pres.9.2a_tmp.md" "pres.9.2a.toc" [541] "pres.9.2a.vrb" "pres.9.3a.aux" "pres.9.3a.html" [544] "pres.9.3a.log" "pres.9.3a.nav" "pres.9.3a_notes.aux" [547] "pres.9.3a_notes.log" "pres.9.3a_notes.out" "pres.9.3a_notes.pdf" [550] "pres.9.3a_notes.tex" "pres.9.3a_notes.toc" "pres.9.3a.out" [553] "pres.9.3a.pdf" "pres.9.3a.R" "#pres.9.3a.Rmd#" [556] "pres.9.3a.Rmd" "pres.9.3a.Rmd~" "pres.9.3a.snm" [559] "pres.9.3a.tex" "pres.9.3a_tmp.md" "pres.9.3a.toc" [562] "pres.9.3a.vrb" "pres.9.4a.aux" "pres.9.4a.html" [565] "pres.9.4a.log" "pres.9.4a.nav" "pres.9.4a_notes.aux" [568] "pres.9.4a_notes.log" "pres.9.4a_notes.out" "pres.9.4a_notes.pdf" [571] "pres.9.4a_notes.tex" "pres.9.4a_notes.toc" "pres.9.4a.out" [574] "pres.9.4a.pdf" "pres.9.4a.R" "#pres.9.4a.Rmd#" [577] "pres.9.4a.Rmd" "pres.9.4a.snm" "pres.9.4a.tex" [580] "pres.9.4a_tmp.md" "pres.9.4a.toc" "pres.9.4a.vrb" [583] "pres.b1.aux" "pres.b1.html" "pres.b1.log" [586] "pres.b1_notes.aux" "pres.b1_notes.log" "pres.b1_notes.out" [589] "pres.b1_notes.pdf" "pres.b1_notes.tex" "pres.b1_notes.toc" [592] "pres.b1.out" "pres.b1.pdf" "pres.b1.R" [595] "#pres.b1.Rmd#" "pres.b1.Rmd" "pres.b1.Rmd~" [598] "pres.b1.tex" "pres.b1_tmp.md" "pres.b1.vrb" [601] "quiz.aux" "quiz.html" "quiz.log" [604] "quiz.nav" "quiz_notes.aux" "quiz_notes.log" [607] "quiz_notes.out" "quiz_notes.pdf" "quiz_notes.tex" [610] "quiz_notes.toc" "quiz.out" "quiz.pdf" [613] "quiz.R" "quiz.Rmd" "quiz.Rmd~" [616] "quiz.snm" "quiz.tex" "quiz_tmp.md" [619] "quiz.toc" "*shell*" "*shell*~" [622] "#*shell*#" "#test.bugs#" "test.bugs" [625] "test.png" "texput.log" "xkcd.png" [628] "xkcd.ttf"
Basic usage
Q u i t t i n g
> q()
NOTE - do not put such a command in a script!
Basic syntax
F u n c t i
- n
s
- collections of commands that expand the
syntax of R
- perform a single action
- parameters that alter behaviour
Basic syntax
F u n c t i
- n
s
seq() - a function that generates sequences
> seq(from=2,to=10) [1] 2 3 4 5 6 7 8 9 10
Basic syntax
F u n c t i
- n
s
seq() - a function that generates sequences
> seq(from=2,to=10) [1] 2 3 4 5 6 7 8 9 10 function (from = 1, to = 1, by = ((to - from)/(length.out - 1)), length.out = NULL, along.with = NULL, ...)
- type seq( and then hit the TAB key
Basic syntax
F u n c t i
- n
s
seq()
> seq(from=2,to=10) [1] 2 3 4 5 6 7 8 9 10 > seq(from=2,to=10,by=2) [1] 2 4 6 8 10
Basic syntax
F u n c t i
- n
s
seq()
> seq(from=2,to=10) [1] 2 3 4 5 6 7 8 9 10 > seq(from=2,to=10,by=2) [1] 2 4 6 8 10 > seq(from=2,to=10,length.out=3) [1] 2 6 10
Basic syntax
F u n c t i
- n
s
seq()
> seq(from=2,to=10) [1] 2 3 4 5 6 7 8 9 10 > seq(from=2,to=10,by=2) [1] 2 4 6 8 10 > seq(from=2,to=10,length.out=3) [1] 2 6 10
Unique parameter identifiers
> seq(from=2,to=10,l=3) [1] 2 6 10
Your turn
- generate a sequence of 10 numbers that
increments by 2 and starting at 8
Your turn
- generate a sequence of 10 numbers that
increments by 2 and starting at 8
> seq(from=8, len=10, by=2) [1] 8 10 12 14 16 18 20 22 24 26
Basic syntax
O v e r l
- a
d e d F u n c t i
- n
s
seq()
> apropos("^seq\\.") [1] "seq.Date" "seq.default" "seq.int" "seq.POSIXt" > str(seq.default) function (from = 1, to = 1, by = ((to - from)/(length.out - 1)), length.out = NULL, along.with = NULL, ...) > str(seq.Date) function (from, to, by, length.out = NULL, along.with = NULL, ...)
Basic syntax
O v e r l
- a
d e d F u n c t i
- n
s
seq()
> # create a sequence of dates spaced 7 days apart between 29th Feb 2000 and 30th Apr 2000 > sampleDates <- seq(from=as.Date("2000-02-29"), + to=as.Date("2000-04-30"), by="7 days") > # print (view) these dates > sampleDates [1] "2000-02-29" "2000-03-07" [3] "2000-03-14" "2000-03-21" [5] "2000-03-28" "2000-04-04" [7] "2000-04-11" "2000-04-18" [9] "2000-04-25"
Basic syntax
O v e r l
- a
d e d F u n c t i
- n
s
> mean(c(1,2,3,4)) [1] 2.5 > mean(sampleDates) [1] "2000-03-28"
Getting help
> help(mean)
Getting help
> help(mean) > ?mean
Getting help
> help(mean) > ?mean > example(mean)
Getting help
D e m
- n
s t r a t i
- n
s
> demo(graphics) #run the graphics demo > demo() #list all demos available on your system
Getting help
S e a r c h i n g
> apropos('mea')
Getting help
S e a r c h i n g
> apropos('mea') > help.search('mean') #search the local R manuals > help.start() #search the local HTML R manuals
Getting help
S e a r c h i n g
> apropos('mea') > help.search('mean') #search the local R manuals > help.start() #search the local HTML R manuals
Getting help
F u n c t i
- n
a r g u m e n t s
> args(mean) #the arguments that apply to the mean function function (x, ...) NULL > args(mean.default) function (x, trim = 0, na.rm = FALSE, ...) NULL > args(list.files) #the arguments that apply to the list.files function function (path = ".", pattern = NULL, all.files = FALSE, full.names = FALSE, recursive = FALSE, ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE) NULL
and dont forget the auto-complete
Package management
P a c k a g e s
Loaded packages
> search() [1] ".GlobalEnv" "package:knitr" "package:stats" [4] "package:graphics" "package:grDevices" "package:utils" [7] "package:datasets" "package:methods" "Autoloads" [10] "package:base"
Package management
P a c k a g e s
Loading packages
> library(MASS) > search() [1] ".GlobalEnv" "package:MASS" "package:knitr" [4] "package:stats" "package:graphics" "package:grDevices" [7] "package:utils" "package:datasets" "package:methods" [10] "Autoloads" "package:base"
Package management
P a c k a g e s
Loading packages
> library(MASS) > search()
Unloading packages
> detach("package:MASS") > search() [1] ".GlobalEnv" "package:knitr" "package:stats" [4] "package:graphics" "package:grDevices" "package:utils" [7] "package:datasets" "package:methods" "Autoloads" [10] "package:base"
Package management
L i s t i n g i n s t a l l e d p a c k a g e s
> installed.packages() > installed.packages(fields = + c("Package", "LibPath", "Version", "Depends", "Built"))
Package management
A v a i l a b l e p a c k a g e s
> available.packages()
I n s t a l l i n g p a c k a g e s
> install.packages()
Package management
A v a i l a b l e p a c k a g e s
> available.packages()
I n s t a l l i n g p a c k a g e s
> install.packages()
- For example, to install the car package
> install.packages('car')
Package management (Rstudio)
A v a i l a b l e p a c k a g e s
Package management (Rstudio)
I n s t a l l i n g p a c k a g e s
Data types (atomic modes)
P r i m a r y d a t a t y p e s
Type Description integer whole numbers numeric numbers (floating points) character words logical TRUE/FALSE (0/1)
Data types
D e r i v e d d a t a t y p e s
Type Description factor categorical variable date (Date) number of days since 1970-01-01 POSIX (Time and Date) number of seconds since 1900-01-01 00:00:00
Dates
> Date <- c('2000-02-29','2002-08-20','2004-02-21') > (Dates<-as.Date(Date)) [1] "2000-02-29" "2002-08-20" "2004-02-21" > mean(Dates) [1] "2002-04-24"
- But what about other formats!
Dates
> as.Date('29/02/2000', format='%d/%m/%Y') [1] "2000-02-29" > as.Date('29th Feb 2000', format='%dth %b %Y') [1] "2000-02-29"
check out the help for strptime
Dates and times
> as.POSIXct('29/02/2000 07:22:30', format='%d/%m/%Y %H:%M:%S') [1] "2000-02-29 07:22:30 AEST"
Formatting dates and times
> Dates [1] "2000-02-29" "2002-08-20" "2004-02-21" > format(Dates,format='%d/%m/%Y') [1] "29/02/2000" "20/08/2002" "21/02/2004" > format(Dates,format='%b %Y') [1] "Feb 2000" "Aug 2002" "Feb 2004" > format(Dates,format='%Y') [1] "2000" "2002" "2004"
Dates and times
packages: lubridate
> Date [1] "2000-02-29" "2002-08-20" "2004-02-21" > library(lubridate) > ymd(Date) [1] "2000-02-29 UTC" "2002-08-20 UTC" "2004-02-21 UTC" > dmy('29/02/2000') [1] "2000-02-29 UTC"
Dates and times
packages: lubridate
> Dates [1] "2000-02-29" "2002-08-20" "2004-02-21" > library(lubridate) > year(Dates) [1] 2000 2002 2004 > class(Date) [1] "character" > decimal_date(Dates) [1] 2000.161 2002.633 2004.139 > week(Dates) [1] 9 34 8
Dates and times
packages: lubridate
> ddays(100) [1] "8640000s (~100 days)" > Dates+100 [1] "2000-06-08" "2002-11-28" "2004-05-31" > Dates+ddays(100) [1] "2000-06-08" "2002-11-28" "2004-05-31" > Dates+dweeks(5) [1] "2000-04-04" "2002-09-24" "2004-03-27"
Dates and times
packages: lubridate
> dmy_hms('29/02/2000 07:22:30') [1] "2000-02-29 07:22:30 UTC" > dmy_hms('29/02/2000 07:22:30')+100 [1] "2000-02-29 07:24:10 UTC" > dmy_hms('29/02/2000 07:22:30')+ddays(100) [1] "2000-06-08 07:22:30 UTC" > quarter(Dates) [1] 1 3 1
Storage types
Type Description vector 1-d array (same type) matrix 2-d array (same type and length) list collection of vectors data frame 2-d array (any type, same length)
Data types
V e c t
- r
s
> # a numeric vector > TEMPERATURE <- c(36.1, 30.6, 31, 36.3, 39.9, 6.5, 11.2, 12.8, 9.7, 15.9) > TEMPERATURE [1] 36.1 30.6 31.0 36.3 39.9 6.5 11.2 12.8 9.7 [10] 15.9
Data types
V e c t
- r
s
> # a numeric vector > TEMPERATURE <- c(36.1, 30.6, 31, 36.3, 39.9, 6.5, 11.2, 12.8, 9.7, 15.9) > TEMPERATURE [1] 36.1 30.6 31.0 36.3 39.9 6.5 11.2 12.8 9.7 [10] 15.9 > # a character vector > WORDS<-c('Fish', 'Rock', 'Tree', "Git") > WORDS [1] "Fish" "Rock" "Tree" "Git"
Data types
V e c t
- r
s
> # a numeric vector > TEMPERATURE <- c(36.1, 30.6, 31, 36.3, 39.9, 6.5, 11.2, 12.8, 9.7, 15.9) > TEMPERATURE [1] 36.1 30.6 31.0 36.3 39.9 6.5 11.2 12.8 9.7 [10] 15.9 > # a character vector > WORDS<-c('Fish', 'Rock', 'Tree', "Git") > WORDS [1] "Fish" "Rock" "Tree" "Git" > # a boolean vector > BOOL<-c(TRUE, TRUE, FALSE, TRUE) > BOOL [1] TRUE TRUE FALSE TRUE
Data types
V e c t
- r
s
Regular sequences
> rep(4,5) [1] 4 4 4 4 4 > rep('Fish',5) [1] "Fish" "Fish" "Fish" "Fish" "Fish"
Data types
V e c t
- r
s
paste()
> QUADRATS <- c("Q1","Q2","Q3","Q4","Q5","Q6","Q7","Q8","Q9","Q10") > QUADRATS [1] "Q1" "Q2" "Q3" "Q4" "Q5" "Q6" "Q7" [8] "Q8" "Q9" "Q10"
Data types
V e c t
- r
s
paste()
> QUADRATS <- c("Q1","Q2","Q3","Q4","Q5","Q6","Q7","Q8","Q9","Q10") > QUADRATS [1] "Q1" "Q2" "Q3" "Q4" "Q5" "Q6" "Q7" [8] "Q8" "Q9" "Q10" > QUADRATS <- paste("Q",1:10,sep="") > QUADRATS [1] "Q1" "Q2" "Q3" "Q4" "Q5" "Q6" "Q7" [8] "Q8" "Q9" "Q10"
Data types
V e c t
- r
s
properties
> TEMPERATURE [1] 36.1 30.6 31.0 36.3 39.9 6.5 11.2 12.8 9.7 [10] 15.9 > names(TEMPERATURE) NULL
Data types
V e c t
- r
s
properties
> TEMPERATURE [1] 36.1 30.6 31.0 36.3 39.9 6.5 11.2 12.8 9.7 [10] 15.9 > names(TEMPERATURE) NULL > names(TEMPERATURE) <- QUADRATS > TEMPERATURE Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 36.1 30.6 31.0 36.3 39.9 6.5 11.2 12.8 9.7 15.9
Data types
V e c t
- r
s
> SITE <- paste(rep(LETTERS[1:5], each = 2), 1:2, sep = "") > SITE [1] "A1" "A2" "B1" "B2" "C1" "C2" "D1" "D2" "E1" [10] "E2"
Your turn
Calculate the area of circles with the following radii:
- circle A=10cm
- circle B=12.3cm
- circle C=25.6cm and store the results in a
vector with item names reflecting the circle names
Your turn
> RADIUS <- c(10,12.3,25.6) > names(RADIUS) <- c('circle A', 'circle B', 'circle C') > #OR > names(RADIUS) <- paste('circle',LETTERS[1:3]) > #OR even better > names(RADIUS) <- paste('circle',LETTERS[1:length(RADIUS)]) > AREA <- pi*RADIUS^2 > AREA circle A circle B circle C 314.1593 475.2916 2058.8742
Data types
V e c t
- r
s
Factors
> SHADE <- rep(c("no","full"),each=5) > SHADE [1] "no" "no" "no" "no" "no" "full" [7] "full" "full" "full" "full"
Data types
V e c t
- r
s
Factors
> SHADE <- rep(c("no","full"),each=5) > SHADE [1] "no" "no" "no" "no" "no" "full" [7] "full" "full" "full" "full" > SHADE <- factor(SHADE) > SHADE [1] no no no no no full full full full [10] full Levels: full no
Data types
V e c t
- r
s
Factors
> SHADE <- rep(c("no","full"),each=5) > SHADE [1] "no" "no" "no" "no" "no" "full" [7] "full" "full" "full" "full" > SHADE <- factor(SHADE) > SHADE [1] no no no no no full full full full [10] full Levels: full no > SHADE <- factor(SHADE, levels=c("no","full")) > SHADE [1] no no no no no full full full full [10] full Levels: no full
Data types
V e c t
- r
s
Factors
> SHADE <- gl(2,5,10,c("no","full")) > SHADE [1] no no no no no full full full full [10] full Levels: no full
Your turn
Create a categorical vector with: - three levels (A, B and C) - four replicates of each level
Your turn
> gl(3,4,12,lab=LETTERS[1:3]) [1] A A A A B B B B C C C C Levels: A B C
But what if you needed to arrange such that there was only two replicates in a row??
Your turn
> gl(3,2,12,lab=LETTERS[1:3]) [1] A A B B C C A A B B C C Levels: A B C
Data types
M a t r i c e s
> matrix(SHADE,nrow=5) [,1] [,2] [1,] "no" "full" [2,] "no" "full" [3,] "no" "full" [4,] "no" "full" [5,] "no" "full"
Data types
M a t r i c e s
> X <- c(16.92,24.03,7.61,15.49,11.77) > Y<- c(8.37,12.93,16.65,12.2,13.12) > XY <- cbind(X,Y) > XY X Y [1,] 16.92 8.37 [2,] 24.03 12.93 [3,] 7.61 16.65 [4,] 15.49 12.20 [5,] 11.77 13.12
Data types
M a t r i c e s
> X <- c(16.92,24.03,7.61,15.49,11.77) > Y<- c(8.37,12.93,16.65,12.2,13.12) > XY <- cbind(X,Y) > XY X Y [1,] 16.92 8.37 [2,] 24.03 12.93 [3,] 7.61 16.65 [4,] 15.49 12.20 [5,] 11.77 13.12 > rbind(X,Y) [,1] [,2] [,3] [,4] [,5] X 16.92 24.03 7.61 15.49 11.77 Y 8.37 12.93 16.65 12.20 13.12
Data types
M a t r i c e s
Beware of mixing data types
> cbind(TEMPERATURE,SITE) TEMPERATURE SITE Q1 "36.1" "A1" Q2 "30.6" "A2" Q3 "31" "B1" Q4 "36.3" "B2" Q5 "39.9" "C1" Q6 "6.5" "C2" Q7 "11.2" "D1" Q8 "12.8" "D2" Q9 "9.7" "E1" Q10 "15.9" "E2"
Data types
M a t r i c e s
Beware of mixing data types
> cbind(TEMPERATURE,SHADE) TEMPERATURE SHADE Q1 36.1 1 Q2 30.6 1 Q3 31.0 1 Q4 36.3 1 Q5 39.9 1 Q6 6.5 2 Q7 11.2 2 Q8 12.8 2 Q9 9.7 2 Q10 15.9 2
Data types
L i s t s
> EXPERIMENT <- list(SITE=SITE,QUADRATS = QUADRATS, + COORDINATES = XY, SHADE = SHADE, + TEMPERATURE = TEMPERATURE) > EXPERIMENT $SITE [1] "A1" "A2" "B1" "B2" "C1" "C2" "D1" "D2" "E1" [10] "E2" $QUADRATS [1] "Q1" "Q2" "Q3" "Q4" "Q5" "Q6" "Q7" [8] "Q8" "Q9" "Q10" $COORDINATES X Y [1,] 16.92 8.37 [2,] 24.03 12.93 [3,] 7.61 16.65 [4,] 15.49 12.20 [5,] 11.77 13.12 $SHADE [1] no no no no no full full full full [10] full Levels: no full $TEMPERATURE Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 36.1 30.6 31.0 36.3 39.9 6.5 11.2 12.8 9.7 15.9
Data types
L i s t s
Elements in lists (NOTE THE $)
> EXPERIMENT$TEMPERATURE Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 36.1 30.6 31.0 36.3 39.9 6.5 11.2 12.8 9.7 15.9
Data types
D a t a f r a m e s
Special type of list
> DATA <- data.frame(QUADRATS = QUADRATS, + SHADE = SHADE, TEMPERATURE = TEMPERATURE) > DATA QUADRATS SHADE TEMPERATURE Q1 Q1 no 36.1 Q2 Q2 no 30.6 Q3 Q3 no 31.0 Q4 Q4 no 36.3 Q5 Q5 no 39.9 Q6 Q6 full 6.5 Q7 Q7 full 11.2 Q8 Q8 full 12.8 Q9 Q9 full 9.7 Q10 Q10 full 15.9
Object manipulation
O b j e c t i n f
- r
m a t i
- n
> class(TEMPERATURE) [1] "numeric" > mode(TEMPERATURE) [1] "numeric" > class(SHADE) [1] "factor" > mode(SHADE) [1] "numeric" > class(EXPERIMENT) [1] "list" > mode(EXPERIMENT) [1] "list"
Object manipulation
O b j e c t i n f
- r
m a t i
- n
> class(DATA) [1] "data.frame" > mode(DATA) [1] "list" > class(mean) [1] "function" > mode(mean) [1] "function"
Object manipulation
O b j e c t i n f
- r
m a t i
- n
> class(TEMPERATURE) [1] "numeric" > class(SHADE) [1] "factor" > class(DATA) [1] "data.frame" > class(mean) [1] "function" > is.numeric(TEMPERATURE) [1] TRUE > is.character(TEMPERATURE) [1] FALSE
Object manipulation
O b j e c t c
- n
v e r s i
- n
> as.character(TEMPERATURE) [1] "36.1" "30.6" "31" "36.3" "39.9" "6.5" [7] "11.2" "12.8" "9.7" "15.9" > as.matrix(DATA) QUADRATS SHADE TEMPERATURE Q1 "Q1" "no" "36.1" Q2 "Q2" "no" "30.6" Q3 "Q3" "no" "31.0" Q4 "Q4" "no" "36.3" Q5 "Q5" "no" "39.9" Q6 "Q6" "full" " 6.5" Q7 "Q7" "full" "11.2" Q8 "Q8" "full" "12.8" Q9 "Q9" "full" " 9.7" Q10 "Q10" "full" "15.9"
Object manipulation
O b j e c t a t t r i b u t e s
> attributes(XY) $dim [1] 5 2 $dimnames $dimnames[[1]] NULL $dimnames[[2]] [1] "X" "Y"
Object manipulation
O b j e c t a t t r i b u t e s
> attributes(XY) $dim [1] 5 2 $dimnames $dimnames[[1]] NULL $dimnames[[2]] [1] "X" "Y" > dim(XY) [1] 5 2
Object manipulation
O b j e c t a t t r i b u t e s
> attr(XY, "dim") [1] 5 2 > attr(XY, "description") <- "coordinates of quadrats" > XY X Y [1,] 16.92 8.37 [2,] 24.03 12.93 [3,] 7.61 16.65 [4,] 15.49 12.20 [5,] 11.77 13.12 attr(,"description") [1] "coordinates of quadrats"
Object indexing
S u b s e t a v e c t
- r
> TEMPERATURE Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 36.1 30.6 31.0 36.3 39.9 6.5 11.2 12.8 9.7 15.9
- 1. Vector of positive numbers
> TEMPERATURE[2] Q2 30.6 > TEMPERATURE[2:5] Q2 Q3 Q4 Q5 30.6 31.0 36.3 39.9 > TEMPERATURE[c(1,5,6,9)] Q1 Q5 Q6 Q9 36.1 39.9 6.5 9.7
Object indexing
S u b s e t a v e c t
- r
> TEMPERATURE Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 36.1 30.6 31.0 36.3 39.9 6.5 11.2 12.8 9.7 15.9
- 2. Vector of negative numbers
> TEMPERATURE[-2] Q1 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 36.1 31.0 36.3 39.9 6.5 11.2 12.8 9.7 15.9 > TEMPERATURE[-2:-5] Q1 Q6 Q7 Q8 Q9 Q10 36.1 6.5 11.2 12.8 9.7 15.9 > TEMPERATURE[c(1,5,6,9)*-1] Q2 Q3 Q4 Q7 Q8 Q10 30.6 31.0 36.3 11.2 12.8 15.9
Object indexing
S u b s e t a v e c t
- r
> TEMPERATURE Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 36.1 30.6 31.0 36.3 39.9 6.5 11.2 12.8 9.7 15.9
- 3. Vector of character strings
> TEMPERATURE["Q1"] Q1 36.1 > TEMPERATURE[c("Q1","Q4")] Q1 Q4 36.1 36.3
Object indexing
S u b s e t a v e c t
- r
> TEMPERATURE Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 36.1 30.6 31.0 36.3 39.9 6.5 11.2 12.8 9.7 15.9
- 4. Vector of logical values
> TEMPERATURE[TEMPERATURE < 15] Q6 Q7 Q8 Q9 6.5 11.2 12.8 9.7 > TEMPERATURE[SHADE == "no"] Q1 Q2 Q3 Q4 Q5 36.1 30.6 31.0 36.3 39.9 > TEMPERATURE[TEMPERATURE < 34 & SHADE == "no"] Q2 Q3 30.6 31.0 > TEMPERATURE[TEMPERATURE < 10 | SHADE == "no"] Q1 Q2 Q3 Q4 Q5 Q6 Q9 36.1 30.6 31.0 36.3 39.9 6.5 9.7
Your turn
Subset the SITES vector:
- sites that have temperatures between 14
and 31
- sites that have temperature < 10 or no
shade
- full shade sites with temperatures > 14 or
no shade sites with temperatures greater than 38
Your turn
SITE TEMPERATURE SHADE Q1 A1 36.1 no Q2 A2 30.6 no Q3 B1 31.0 no Q4 B2 36.3 no Q5 C1 39.9 no Q6 C2 6.5 full Q7 D1 11.2 full Q8 D2 12.8 full Q9 E1 9.7 full Q10 E2 15.9 full > SITE[TEMPERATURE >= 14 & TEMPERATURE <=31] [1] "A2" "B1" "E2" > SITE[TEMPERATURE < 10 | SHADE=='no'] [1] "A1" "A2" "B1" "B2" "C1" "C2" "E1" > SITE[TEMPERATURE > 14 & SHADE=='full' | (TEMPERATURE > 38 & SHADE=='no')] [1] "C1" "E2"
Your turn
Subset the SITES vector:
SITE TEMPERATURE SHADE Q1 A1 36.1 no Q2 A2 30.6 no Q3 B1 31.0 no Q4 B2 36.3 no Q5 C1 39.9 no Q6 C2 6.5 full Q7 D1 11.2 full Q8 D2 12.8 full Q9 E1 9.7 full Q10 E2 15.9 full
- sites with temperature > 35 or full shade
and temperature < 39
Your turn
SITE TEMPERATURE SHADE Q1 A1 36.1 no Q2 A2 30.6 no Q3 B1 31.0 no Q4 B2 36.3 no Q5 C1 39.9 no Q6 C2 6.5 full Q7 D1 11.2 full Q8 D2 12.8 full Q9 E1 9.7 full Q10 E2 15.9 full > SITE[TEMPERATURE > 35 | SHADE=='full' & TEMPERATURE < 39] [1] "A1" "B2" "C1" "C2" "D1" "D2" "E1" "E2" > SITE[(TEMPERATURE > 35 | SHADE=='full') & TEMPERATURE < 39] [1] "A1" "B2" "C2" "D1" "D2" "E1" "E2"
Basic data formatting
R
- u
n d i n g
> TEMPERATURE Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 36.1 30.6 31.0 36.3 39.9 6.5 11.2 12.8 9.7 15.9 > #round up to nearest integer > ceiling(TEMPERATURE) Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 37 31 31 37 40 7 12 13 10 16 > #round down to nearest integer > floor(TEMPERATURE) Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 36 30 31 36 39 6 11 12 9 15
Basic data formatting
R
- u
n d i n g
> TEMPERATURE Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 36.1 30.6 31.0 36.3 39.9 6.5 11.2 12.8 9.7 15.9 > #round to specified decimal places > round(TEMPERATURE) Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 36 31 31 36 40 6 11 13 10 16 > round(TEMPERATURE/2.2,digits=2) Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 16.41 13.91 14.09 16.50 18.14 2.95 5.09 5.82 Q9 Q10 4.41 7.23 > round(TEMPERATURE,digits=-1) Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 40 30 30 40 40 10 10 10 10 20
Formatting strings
Function Description
paste()
Concatenate vectors after converting into characters
format()
Adjust decimal places, justification, padding and width of string and whether to use scientific notation
formatC()
A version of format() that is compliant with C style formatting
sprintf()
A wrapper for the C style formatting function of the same name, provides even greater flexibility (and complexity)
Formatting strings
p a s t e
> paste("Quadrat", 1:3, sep=":") [1] "Quadrat:1" "Quadrat:2" "Quadrat:3" > #create a joint label for Site and Quadrat combinations > paste(SITE,QUADRATS,sep=":") [1] "A1:Q1" "A2:Q2" "B1:Q3" "B2:Q4" "C1:Q5" [6] "C2:Q6" "D1:Q7" "D2:Q8" "E1:Q9" "E2:Q10" > #create a string of comma separated items > paste('Quad',1:4,sep='',collapse=', ') [1] "Quad1, Quad2, Quad3, Quad4"
Formatting strings
f
- r
m a t
> TEMPERATURE Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 36.1 30.6 31.0 36.3 39.9 6.5 11.2 12.8 9.7 15.9 > #create equal width strings (add padding on the left) > format(TEMPERATURE) Q1 Q2 Q3 Q4 Q5 Q6 Q7 "36.1" "30.6" "31.0" "36.3" "39.9" " 6.5" "11.2" Q8 Q9 Q10 "12.8" " 9.7" "15.9"
Formatting strings
f
- r
m a t
Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 36.1 30.6 31.0 36.3 39.9 6.5 11.2 12.8 9.7 15.9 > #create equal width strings with a minimum of 2 decimal places > format(TEMPERATURE, nsmall=2) Q1 Q2 Q3 Q4 Q5 Q6 "36.10" "30.60" "31.00" "36.30" "39.90" " 6.50" Q7 Q8 Q9 Q10 "11.20" "12.80" " 9.70" "15.90" > #contrast to the following > #truncate to 2 decimal placed before rounding > format(TEMPERATURE, digits=2) Q1 Q2 Q3 Q4 Q5 Q6 Q7 "36.1" "30.6" "31.0" "36.3" "39.9" " 6.5" "11.2" Q8 Q9 Q10 "12.8" " 9.7" "15.9"
Formatting strings
f
- r
m a t
Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 36.1 30.6 31.0 36.3 39.9 6.5 11.2 12.8 9.7 15.9 > #truncate to 1 decimal place before rounding > format(TEMPERATURE, digits=1) Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 "36" "31" "31" "36" "40" " 6" "11" "13" "10" "16" > #scientific notation > format(TEMPERATURE,scientific=TRUE) Q1 Q2 Q3 Q4 "3.61e+01" "3.06e+01" "3.10e+01" "3.63e+01" Q5 Q6 Q7 Q8 "3.99e+01" "6.50e+00" "1.12e+01" "1.28e+01" Q9 Q10 "9.70e+00" "1.59e+01"
Formatting strings
f
- r
m a t
Works for data frames as well
> # character and factor columns left justified > # numerical columns minimum of two decimal places > format(DATA, justify="left", nsmall=2) QUADRATS SHADE TEMPERATURE Q1 Q1 no 36.10 Q2 Q2 no 30.60 Q3 Q3 no 31.00 Q4 Q4 no 36.30 Q5 Q5 no 39.90 Q6 Q6 full 6.50 Q7 Q7 full 11.20 Q8 Q8 full 12.80 Q9 Q9 full 9.70 Q10 Q10 full 15.90
Formatting strings
f
- r
m a t C
> #generate a sequence of numbers > (NUM <- exp(seq(0,10,length=5))) [1] 1.00000 12.18249 148.41316 [4] 1808.04241 22026.46579 > format(NUM,,big.mark = ",",digits=2) [1] " 1" " 12" " 148" " 1,808" "22,026"
Formatting strings
f
- r
m a t C
> #generate a sequence of numbers > (NUM <- exp(seq(-1,10,length=5))) [1] 3.678794e-01 5.754603e+00 9.001713e+01 [4] 1.408105e+03 2.202647e+04 > format(NUM,,big.mark = ",",digits=2) [1] "3.7e-01" "5.8e+00" "9.0e+01" "1.4e+03" [5] "2.2e+04" > formatC(NUM, format='f',big.mark = ",",digits=2) [1] "0.37" "5.75" "90.02" [4] "1,408.10" "22,026.47"
Formatting strings
f
- r
m a t C
- 'd' for integers
- 'f' for reals in the standard xxx.xxx
format
- 'e', 'E' for reals in the scientific
(n.ddde+nn) format
- 'g', 'G' for reals in the scientific
(n.ddde+nn) format when it saves space to do so
Formatting strings
f
- r
m a t C
- 's' for strings
- 'd' for integers