SOME S-PLUS BASICS AND COMMONLY USED COMMANDS
A. STARTUP AND ENVIRONMENT
Splus to start up the Splus system
motif() to open a graphics window for plots on screen
help.start() to open the help window system
or you may need to use help.start(gui="motif")
B. INPUT AND OUTPUT
x <- scan("filename") to read numbers from a file
x <- read.table(file="filename") to read a table (matrix) of
data
source("sfile.s") to read Splus code from a file
sink("file") to write everthing that follows
to a file
sink() ... and back to the
screen
write(..) write to file -- check help
write.table(..) write to file -- check help
C. ORGANIZING DATA
x <- matrix(x,ncol=5,byrow=T) reshapes x to a 5-column matrix,
by row
x <- matrix(x,12,5) reshapes x to a 12x5 matrix, by
column
x <- matrix(scan("filename"),12,5) etc
x <- c(x,c(1,10,y),1:10) c() means "catenate" to create
vectors
D. RANDOM VARIATE GENERATION AND DISTRIBUTIONS
sample to sample with or without replacement
from a set
dnorm() normal pdf
pnorm() normal cdf
qnorm() normal quantile function, inverse of
pnorm()
rnorm() generate normal random variates
?pnorm or help(pnorm) Give syntax help for pnorm() etc.
Others: ?dist where ? =d,p,q,r with parameters as follows:
|+--------------------------------------------------------------+|
|| 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 - ||
|| norm normal mean, sd 0, 1 ||
|| t Student's t df - ||
|| unif uniform min, max 0, 1 ||
|+--------------------------------------------------------------+|
E. PLOTTING
par(mfrow=c(3,2)) Next 6 plots are laid out in 3
rows of 2
par(....) Many arguments to control display,
such as
mfrow=c(1,1) plot layout on screen/page
bty="n" no frame drawn around graph
... see "help(par)" for others
hist(x,nclass=25,prob=T) histogram with 25 "classes",
normalized
as a pdf (i.e. with area 1)
plot(x,y) scatter plot (points with coord
x,y)
plot(x,y,type="l") line plot (connect-the-dots)
plot(x,y, ...) many arguments to control display,
such as
type="l" for lines
xlim=c(0,10) horizontal range of plot
ylim=c(0,1) vertical " " "
xlab="here is a label on the x-axis"
ylab=...
col=2 use second color (could be 1-15)
lty=3 use third (broken) line type (lots
avail)
lwd=2 twice the usual line width
lines(x, y) add lines to an existing plot
points(x, y) add points to an existing plot
tsplot(x) "Time Series" Plot a vector vs
1,2,...
qqnorm(x) normal quantile-quantile plot
boxplot(x, ..) boxplot
mtext(side=3, line=0, cex=2, outer=T,
"This is an Overall Title For the Page")
F. ASSIGNMENT AND BASIC ARITHMETIC OPERATORS
<- Assignment (note: S-Plus does NOT use "=" for assignment)
* Multiply
+ Add
- Subtract
/ Divide
^ Exponentiate
G. SEQUENCE AND REPETITION
x <- 1:50 1,2,3,...,49,50
x <- seq(1,50,by=10) 1,11,21,31,41
x <- seq(1,50,length=50) 1,2,3,...
x <- seq(0,1,length=101)
0,0.01,0.02,0.03,...,0.99,1.00
x <- rep(y,10) y,y,y,y,y,y,y,y,y,y
H. SUBSCRIPTS
[ ] Vector subscript x[3]<-101; y <- x[20:1]+1
[,] Matrix subscript x[1,5]<-0; y[1:10,]
I. RELATIONAL OPERATORS
== Equal-to (4==7) is 0; (1+2==3) is 1
!= Not-equal-to (4==7) is 1; (1+2==3) is 0
< Less-than
<= Less-than-or-equal-to
> Greater-than
>= Greater-than-or-equal-to
J. CONDITIONALS
if (i==1) x<-10
if (i>0) { x<-10; y<-20}
else x<-y<-0
K. ITERATION
for (i in 1:10) { x[i]<-i; y<-c(i,y); ... }
L. ARTIHMETIC OPERATORS AND FUNCTIONS
abs(x) Absolute value
cos(x), sin(x), acos(x), etc Trig functions
exp(x) Exponential function
gamma(x) (x-1)!
log(x) Default base is e, not 10
log(x, base=exp(1)) Optional argument sets base
max(...), min(...) Maximum, minimum
mean(x) Sample average
median(x) and median
mode(x) and mode
var(x) and variance
summary(x) Gives summary statistics
quantile(x, probs=c(0,.25,.5,.75,1)) Your choice of quantiles
sum(...) Add 'em up
var(x,y) Covariance, with 2 arguments
cor(x,y) Correlation coefficient