[Introduction | Getting help | Common Commands | Types of objects | Input and output of data | Programs | Some arithmetic commands | Plotting data| Customizing your session |] |
S-plus is a versatile package which can be used for analyzing data using the pre-packaged software, and also has its own programing laguage, which can be used to write your own routines. The comments I have written here apply to S-PLUS version 5.1 . Splus is case sensitive, it matters whether the letters is lower or uppercase.
To start Splus, you type Splus or Splus -e. When you are in Splus, you will have the sign >. If a commands extends over one line of input, the prompt changes to the plus sign: +. To exit you type q(). Next, there is an example of a session.
agate% Splus -e S-PLUS : Copyright (c) 1988, 1999 MathSoft, Inc. S : Copyright Lucent Technologies, Inc. Version 5.1 Release 1 for Linux 2.0.31 : 1999 Working data will be in /home/arcones/MySwork > a_c(1,2,3) > a [1] 1 2 3 > b_c(scan()) 1: 12 13 15 4: > b [1] 12 13 15 > c1_a*b > c1 [1] 12 26 45 y <- c("a", "b", "d") > q()
> help("filename")or
> ?filenameYou can get a separate window with the help information doing:
> help("filename",window=T)You can general information on Splus by doing:
> help.start()You get all Functions and Datasets.
- >ls()
- >objects()
- List objects in your working directory. This can be used with pattern matching using unix regular expressions.
- >rm (x)
- Remove object x. This can also take pattern arguments in the form of regular expressions. Unless you remove an object, Splus will keep the object from one sesion to another.
- >length(x)
- Returns the length (number of elements) of the object x.
- >dim(x)
- Returns the dimensions of an object. In the case of matrices, the first element is the number of rows in the matrix and the second element is the number of columns.
- >nrow(x)
- Return the number of rows in a matrix.
- >ncol(x)
- Return the number of columns in a matrix.
- > history()
- Allows to repeat a previous used command.
- > again()
- Repeat the last used command.
- > options()
- See the list of options.
- >!
- Enter a unix command. For example,
- >!lpr
- Enter the unix command lpr.
A vector is a sequence of real numbers or categorical variables. For example:
> a2_c(12,14,16) > a2 [1] 12 14 16 > a3_c("TX","NY","TN","CA") > a3 [1] "TX" "NY" "TN" "CA"It is possible to name a vector
> heights <- c(160,140,155,170) > names(heights) <- c("Ned","Jill","Pat","Ronnie") > heights Ned Jill Pat Ronnie 160 140 155 170Alternatively, you can do
> people<- c("Ned","Jill","Pat","Ronnie") > names(heights) <- people > heights Ned Jill Pat Ronnie 160 140 155 170To create a matrix, you can do:
> a2_c(42, 66, 40, 62, 42, 71, 43, 79, 42, 67) > kids_matrix(a2,ncol=2,byrow=T) > kids [,1] [,2] [1,] 42 66 [2,] 40 62 [3,] 42 71 [4,] 43 79 [5,] 42 67To name the rows and columns of a matrix, you can do as follow:
> dimnames(kids)_list(c("Tom","Fred","Sue","Donna","Lee"), + c("Height","Weight")) > kids Height Weight Tom 42 66 Fred 40 62 Sue 42 71 Donna 43 79 Lee 42 67If you do not want to name the columns, you can use NULL:
> dimnames(kids)_list(c(NULL),c("Height","Weight")) > kids Height Weight [1,] 42 66 [2,] 40 62 [3,] 42 71 [4,] 43 79 [5,] 42 67
> b_c(scan("data")) > b [1] 12 13 14 15You also can input data by doing
> a_c(1,2,3) > a [1] 1 2 3 > b_c(scan()) 1: 12 13 15 4:Doing this you can create variables which will stay in your directory from one session to another.
A simple way to output Splus objects to an ASCII file is to use the function "sink()". For example, the following commands
>sink("outfile") >mydata >grades >sink()forward output that usually appear on screen to the file "outfile". The last "sink()" stop the output to file "outfile", and resumes the output to screen. To change a coordinate of a vector, you can do "a[i]<- k" i is the number of coordinate, k is the number to appear in that coordinate. For, example,
> a [1] 42 52 48 58 4 5 4 3 > a[3]<- -2 > a [1] 42 52 -2 58 4 5 4 3 > a[3]_-4 > a [1] 42 52 -4 58 4 5 4 3
>source("myprogram")For example, the program finds the 95 % confidence interval of a binomial distribution
x_115 n_300 p_x/n z_qnorm(0.975) c1[1]_p-z*sqrt(p*(1-p)/n) c1[2]_p+z*sqrt(p*(1-p)/n) dfr1_2*(n+1-x) dfr2_2*x q1_qf(.975,df1=dfr1,df2=dfr2) c2[1]_(x/(x+((n+1-x)*q1))) dfr1_2*(x+1) dfr2_2*(n-x) q2_qf(.975,df1=dfr1,df2=dfr2) c2[2]_((x+1)*q2)/(n-x+((x+1)*q2))If the program is large, one may wish to put the execution in a batch mode from outside Splus. That is, at Unix prompt, do
Splus BATCH myprogram outfilewhere "outfile" is the name of the file where the output of the program goes.
The following commands create a program to find (n choose k)
> comb_function(n,k) + {if (k==0) + {1} + else{(prod(c(n-k+1:k)))/(prod(c(1:k)))} + } > comb(10,2) [1] 45
a+b (a-b, a*b, a/b) is a vector with the sum (or whatever) of the coordinates of a and b. To get this in a new vector, we can do d_a+b or d<-a+b
> motif()or
> trellis.device()Yo need to open the graphics window before asking for the graph of something. Some basic graphics commands are: barplot, boxplot, contour, coplot, dotchart, faces, hist, pairs, persp, pie, ,plclust, plot, qqnorm, qqplot, tsplothist(x) plot(x,y), usa(). The command
> usa()gives the USA map. To quit graphs you do
> dev.off()
> .First <- + function() + { + options(editor="/opt/local/bin/emacs") + length(50) + digits=17 + help.start(browser="/opt/local/netscape") + }