R Command Symmary
Assignment
expr <- expr
expr = expr
expr -> expr
Conditional
if ( expr ) expr
if ( expr ) expr else expr
Iteration, Flow control
repeat expr
while ( expr ) expr
for ( Name in expr ) expr
Flow
break
next
return ( expr )
( expr )
{ exprlist }
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
Relational Operators
!= Not-equal-to
< L ess-than
<= Less-than-or-equal-to
== Equal
> Greater-than
>= G reater-than-or-equal-to
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)
Above “shortcut” means that if the condition is not satisfied, the remaining parts of the expression are not even
evaluated. This is very useful if these parts don’t even make sense then, or are slow to execute.
Subscripts
[ ] Vector subscript
[[ ]] list subscript - can only identify a single element
$ Named component selection from a list
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
Sequence and Repetition
seq (from, to, by, length, along)
also : as in 1:10
rep(x, times, length)
Arithmetic 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)
Types
Can be used in as. and is. and (length)
array
category is, as only
character
complex
double
integer
list
logical
matrix
null is, as only
numeric
I/O
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)
Command File In
source(file, local = F)
Screen Output to File
sink(file)
sink( ) restores output to screen
Write and Read Objects
dput(x, file) writes out object in S notation
dget(file)
write(t(matrix),file,ncol=ncol(matrix),append=FALSE)
data.dump(objnames, file=’filename’) objnames may be ’obj name’
or c(’name1’,’name2’,...)
data.restore(’filename’)
Make Things Available or Unavailable
assign("name", value, frame, where)
attach(file, pos=2)
detach(2)
library( )
library(help=section)
library(section, first=TRUE) make library’s functions take precedence
help(name="help", offline=F)
args(name="help")
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)
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
Plotting
Device-Specification
postscript(file, command, horizontal=F, width, height, rasters, pointsize=14, font=1, preamble=ps.preamble, fonts=ps.fonts)
x11()
jpeg()
png()
quartz()
Stopping Plotting
graphics.off()
dev.off()
Some Plot Parameters
log=’<x|y|xy>’ Logarithmic axes
main=’title’
new= 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)
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)
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)
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)
Multiple Plots Per Page
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")
Comments (0)
You don't have permission to comment on this page.