R Syntax Overview (modified from that of Barry W. Brown). I. EXPRESSIONS (expr is an expression of your choice) A. Literals number 1 1.1 1.1e10 string 'string' or "string" comment # string. function (formals) expr function(args){defn} B. Calls expr infix expr expr %anything% expr unary expr expr ( arglist ) expr [ arglist ] expr [[ arglist ]] expr $ fname C. Assignment expr <- expr expr -> expr expr = expr D. Conditional if ( expr ) expr if ( expr ) expr else expr E. Iteration while ( expr ) expr for ( Name in expr ) expr II. ARITHMETIC OPERATORS * Multiply + Add - Subtract / Divide ^ Exponentiation %% Remainder or modulo operator %*% Matrix multiplication operator %/% Integer divide %c% crossproduct %o% Outer Product III. RELATIONAL OPERATORS != Not-equal-to < Less-than <= Less-than-or-equal-to == Equal > Greater-than >= Greater-than-or-equal-to IV. LOGICAL OPERATORS ! Not | Or (Use with arrays or matrices) || Shortcut Or (Don't use with arrays or matrices) & And (Use with arrays or matrices) && Shortcut And (Don't use with arrays or matrices) V. SUBSCIPTS [ ] Vector subscript [[ ]] list subscript - can only identify a single element $ Named component selection from a list VI. SEQUENCE AND REPETITION seq (from, to, by, length, along) also : as in 1:10 rep(x, times, length) VII. ARTIHMETIC OPERATORS AND FUNCTIONS abs(x) acos(x) acosh(x) asin(x) asinh(x) atan(x) atan(x, y) atanh(x) ceiling(x) cos(x) cosh(x) exp(x) floor(x) gamma(x) lgamma(x) log(x, base=exp(1)) log10(x) max(...) elementwise min(...) elementwise pmax(...) parallel pmin(...) parallel sin(x) sinh(x) sqrt(x) tan(x) tanh(x) trunc(x) VIII. TYPES Can be used in as. and is. and (length=n calls. array category is, as only character complex double integer list logical matrix null is, as only numeric IX. IN AND OUT OF S A. Data In scan(file="", what=numeric(), n, sep) read.table(file="") B. Command File In source(file, local = F) C. Screen Output to File sink(file) sink( ) restores output to screen E. Make Things (Including Help) Available or Unavailable assign("name", value, frame, where) attach(file, pos=2) detach(what=2) library( ) help(name="help") args(name="help") X. REDUCTION OPERATORS all(...) any(...) length(x) max(...) mean(x, trim=0) median(x) min(...) mode(x) prod(...) quantile(x, probs=c(0,.25,.5,.75,1)) sum(...) var(x,y) cor(x,y,trim=0) XI. STATISTICAL DISTRIBUTIONS d(x,) density at x p(x,) cumulative distn fn to x q(p,) inverse cdf r(n,) generates n random numbers from distn +--------------------------------------------------------------+ | Distribution Parameters Defaults | +--------------------------------------------------------------+ | beta beta shape1, shape2 -, - | | cauchy Cauchy loc, scale 0, 1 | | chisq chi-square df - | | exp exponential - - | | f F df1, df2 -, - | | gamma Gamma shape - | | lnorm log-normal mean, sd (of log) 0, 1 | | logis logistic loc, scale 0, 1 | | norm normal mean, sd 0, 1 | | stab stable index, skew -, 0 | | t Student's t df - | | unif uniform min, max 0, 1 | +--------------------------------------------------------------+ XII. PLOTTING A. postscript(file, command, horizontal=F, width, height, rasters, pointsize=14, font=1, preamble=ps.preamble, fonts=ps.fonts) B. pdf((file = ifelse(onefile, "Rplots.pdf", "Rplot%03d.pdf"), width = 6, height = 6, onefile = TRUE, ...) C. Some Plot Parameters main='title' new= T forces addition to current plot lty=n Line type pch='.' Plot character xlab='x-axis label' ylab='y-axis label' xlim=c(xlo.value,xhi.value) ylim=c(ylo.value,yhi.value) D. One-Dimension Plots barplot(height) #simple form barplot(height, width, names, space=.2, inside=TRUE, beside=FALSE, horiz=FALSE, legend, angle, density, col, blocks=TRUE) boxplot(..., range, width, varwidth=FALSE, notch=FALSE, names, plot=TRUE) hist(x, nclass, breaks, plot=TRUE, angle, density, col, inside) E. Two-Dimension Plots lines(x, y, type="l") points(x, y, type="p")) abline(coef) abline(a, b) abline(reg) abline(h=) abline(v=) qqplot(x, y, plot=TRUE) qqnorm(x, datax=FALSE, plot=TRUE) F. Three-Dimension Plots contour(x, y, z, v, nint=5, add=FALSE, labex) persp(z, eye=c(-6,-8,5), ar=1) XIII. WRITING FUNCTIONS A. tr <- function(in.mat) sum(diag(in.mat)) B. plot.box <- function(x1,y1,x2,y2) { segments(x1,y1,x1,y2) segments(x1,y2,x2,y2) segments(x2,y2,x2,y1) segments(x2,y1,x1,y1) } C. Returning can be done with a final declaration or the return() statement. D. Note that functions can (and should) call other functions.