Jump to my Home Page Send me a message Check out stuff on GitHub Check out my photography on Instagram Check out my profile on LinkedIn Check me out on Facebook

Example R Programs

This collection of example code originates, for the most part, from a few Q&A seminars with new R users in 2015. The participants would ask 'How do I?', and I would do my best to demonstrate how I might do it with R. The files here are cleaned up versions of the transcripts of those R sessions along with added comments. Not a lot of math or statistics here. Mostly just R mechanics. At any rate, I posted them thinking that someone might find them useful.

Plotting

ggplot.R
Some ggplot2 examples to get you started.
colorBrewer.R
Using color brewer colors in R
basePlotStackArea.R
How to draw the horrible stacked area chart with base R
basePlotAnno.R
One way to annotate base R plot margins
basePlotPDF.R
How to get base R plots to create beautiful PDFs

Basic Data Aggregation & Smoothing

smoothing.R
Smoothing data with lowess, medians, and averages
aggr.R
Aggregation of data in a data.frame with base R

C++ (Rcpp) & R

cppInline.R
Define C++ functions in R code with inline
cppRcppInline.R
Define C++ functions in R code with Rcpp
cppRcppOutOfLine.R cppRcppOutOfLine.cpp
Define C++ functions in an external file

C++ (RInside) & R

riMakefile
Build the RInside examples below
riHello.cpp
Our first RInside example
riValues.cpp
Getting computed values from R
riVariables.cpp
Accessing R variables

Power Tools

dplyr.R
dplyr
dataTable.R (dataTable_f1.csv, dataTable_f2.csv.gz, dataTable_f3.txt)
data.table
parallelBasics.R
parallel

Data Manipulation

longAndWide.R
Wide/Tall data: melt, dcast, stack, unstack, & aggregate
merge.R
How to merge data.frames (i.e. SQL join) with base R
sorting.R
How to sort data.frames with base R

Regression

linearRegression.R
Simple linear regression with plots in one variable
polynomialRegression.R
Simple polynomial regression with plots in one variable
nonLinearRegression.R
Nonlinear least squares base R (nls)

Time Series

timeSeriesDecomp.R
time-series decomposition with graphics
timeSeriesSmooth.R
time-series smoothing with graphics

Simple Statistics

outliers.R
Simple outlier detection strategies with base R
tTests.R
Basic t-tests
© 2009 Mitch Richling