R Programming
Different from S? Sweave
01 Background
Resources
- 📖 R for Data Science ⭐️⭐️⭐️⭐️⭐️
- 📖 The Book of R
- 📖 The Art of R Programming
- 📹 Data Science: Foundations using R Specialization by Coursera ⭐️⭐️⭐️
- 🌐 R for Beginners by CRAN
- https://r-graph-gallery.com/
Limited in big data, too big to load in memory
02 Core Concepts
Key Terms
- Facets
- Geoms
- Asethetic
- Atomic: (1) character (2) number (real) (3) integer (4) complex (5) logical Graphs tutorial
- Attributes: (1) names, dimnames (2) dimensions (matrices, arrays) (3) class (4) length (5) other user-defined attributes/metadata
- Coercion Implicit / Explicit
- Matrices
- Factors Ordered / Unordered
- Missing Values NaN is Na, but Na is not NaN
- Data Frames
- Reading Data
read.table
andread.csv
for tabularsource
for reading R code files
- Dput, Dget, Dumping
- Subsetting
- Partial Matching
- https://www.storybench.org/getting-started-data-visualization-r-using-ggplot2/
cacher
package- Plotting
base
,lattice
,ggplot2
,tidyverse
”...
” argument used heavily in generic functions
Lexical Scoping
Complete Cases Times and Dates
as.Date
for datesas.POSIXct
andas.POSIXlt
for timesstrptime
can be used to coerce to the above
Loop Functions (to make looping more easy on the command line)
lapply
look over a list and evaluate a function on each elementsapply
same as lapply but try to simplify the resultapply
apply function over the margins of arraytapply
apply function over subsets of a vectormapply
multivariate version of lappy
Factor Variables
Debugging Tools - Basic Tools
traceback
debug
browser
trace
recover
str
Function
- compactly display the internal structure of an R object
- an alternative to
summary
Simulation
rnorm
generate random Normal variates with a given mean and standard deviationdnorm
evaluate the Normal probability (with a given mean/SD) at a point (or vector of points)pnorm
evaluate the cumulative distribution function for a Normal distributionrpois
generate random Poisson variates with a given rate
R Profiler
system.time()
Rprof
summaryRprof()
Good folder hiearchy
Project.Rproj
Data/
Scripts/
Output/
Knitr
Rpubs
R Markdown
Subsetting
Cross tabs - similar to x table in Excel
Flat Tables