S Cheatsheet
Compiled by:
Barry W. Brown
Department of Biomathematics, Box 237
University of Texas M. D. Anderson Cancer Center
1515 Holcombe Blvd
Houston, TX 77030
bwb@mdaali.cancer.utexas.edu
I. S EXPRESSIONS
A. Literals
number 1 1.1 1.1e10
string 'string' or "string"
name
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
expr <<- expr Forces write to disk
from within a function
D. Conditional
if ( expr ) expr
if ( expr ) expr else expr
E. Iteration
repeat expr
while ( expr ) expr
for ( Name in expr ) expr
F. Flow
break
next
return ( expr )
( expr )
{ exprlist }
II. ARITHMETIC OPERATORS
* Multiply
+ Add
- Subtract
/ Divide
^ Exponentiation
%% Remainder or modulo operator
%*% Matrix multiplication operator
%/% Integer divide
%c% crossproduct m1 %c% m2 is t(m1) %*% m2
%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
V. SUBSCRIPT FORMS
logical extracts or selects T component
positive numbers extracts or selects specified indices
negative numbers deletes specified indices
NA or out of range extends dimensions gives value NA
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.<type> and is.<type> and <type>(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,
multi.line = F, flush = F, append = F)
Example: data <- matrix(scan("data.file"),ncol=5,byrow=T)
B. Command File In
source(file, local = F)
C. Screen Output to File
sink(file)
sink( ) restores output to screen
D. Write and Read Objects
dput(x, file)
dget(file)
write(t(matrix),file,ncol=ncol(matrix),append=FALSE)
dump(list, fileout="dumpdata")
restore(file)
E. Make Things (Including Help) Available or Unavailable
assign("name", value, frame, where)
attach(file, pos=2)
detach(what=2)
library( )
library(help=section)
library(section, first=FALSE)
library.dynam(section, file)
help(name="help", offline=F)
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<dist>(x,<parameters>) density at x
p<dist>(x,<parameters>) cumulative distn fn to x
q<dist>(p,<parameters>) inverse cdf
r<dist>(n,<parameters>) generates n random numbers
from distn
+----------------------------------------------------------------+
|+--------------------------------------------------------------+|
|| <dist> 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. Starting and Stopping Plotting
<device-specification function>
graphics.off()
B. Device-Specification Functions
hp2623(ask=F, file="")
hpgl(width=10, height=7.25, ask=!auto,
auto=F, color=2, speed=400, rotated=F, file="")
postscript(file, command, horizontal=F, width,
height, rasters, pointsize=14, font=1,
preamble=ps.preamble, fonts=ps.fonts)
printer(width=80, height=64, file="", command="")
show()
tek4014(ask=F, file)
sun(ask=FALSE, color=FALSE)
C. Some Plot Parameters
log='<x|y|xy>' Logarithmic axes
main='title'
new=<logical> T forces addition to current plot
sub='bottom title'
type='<l|p|b|n>' Line, points, both, none
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"))
matplot(x, y, type="p", lty=1:5, pch=, col=1:4)
matpoints(x, y, type="p", lty=1:5, pch=, col=1:4)
matlines(x, y, type="l", lty=1:5, pch=, col=1:4)
plot(x, y, type="p", log="")
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)
interp(x, y, z, xo, yo, ncp=0, extrap=FALSE)
persp(z, eye=c(-6,-8,5), ar=1)
G. Multiple Plots Per Page (Example)
par(mfrow=(nrow, ncol), oma=c(0, 0, 4, 0))
mtext(side=3, line=0, cex=2, outer=T,
"This is an Overall Title For the Page")
|
|
|
|
|
|
|
|
||||