The Bare Essentials of R for CEE7430

R is `GNU S', a freely available language and environment for statistical computing and graphics which provides a wide variety of statistical and graphical techniques: linear and nonlinear modelling, statistical tests, time series analysis, classification, clustering, etc. R is distributed by the Comprehensive R Archive Network (CRAN) whose website is http://cran.r-project.org/.  R may be downloaded and installed for your hardware from this site.

R Manuals.  Appendix A in the first manual in this set contains a sample session that can help you get started.

Examples of functionality I find useful in R.

Perform arithmetic.
> 3*(4+7.3)
[1] 33.9

Assign and work with variables
> g=9.81
> m=30
> f=m*g
> f
[1] 294.3

Combine values into a vector
> a=c(1,2,3,4)
> b=a*2
> b
[1] 2 4 6 8

> ls()  # list the objects in your workspace
> rm(f,m,a,b)   # Remove objects (to clean up)

> setwd('d:/')    # Set working directory - note forward slash

Read a data file.  The file boise.txt has been formatted as follows
1911 10 398
1911 11 395
1911 12 398
1912 1 507
1912 2 421
1912 3 530
...
comprising columns giving the year, month and flow.  It is read as follows
> tt=matrix(scan("boise.txt"),ncol=3,byrow=T)
Read 3276 items

> yr=tt[,1]   # put year, month and flow columns in separate vectors
> mo=tt[,2]
> flow=tt[,3]
> ym=yr+mo/12   # a plotting index in fraction of years
> plot(ym,flow,type="l")

Built in functions
 > mean(flow)
[1] 1208.674
> var(flow)
[1] 1800607

Make your own function
sdev=function(x)
{
y=var(x)
return(sqrt(y))
}

Use it
> sdev(flow)
[1] 1341.867

Demonstrate graphics capability
demo(graphics)

Use a package
Packages/Load Package ...  select ts (time series)
acf(flow)  #  autocorrelation function

Back to CEE7430 Page