Вы находитесь на странице: 1из 8

5.

0 Introduction to Splus and R syntax


Notes based on those of Professor Joe Schafer

To invoke Splus, type Splus at the Unix prompt. This will begin your Splus session. The prompt
>

means that Splus is awaiting your commands. Sometimes Splus will give the prompt
+

which means that it is waiting for you to type an expression that is a continuation of the previous line. To exit Splus, type
> q()

which returns you to Unix.

On UNIX, you may also interrupt the session at any time by typing z (read control-z, which is to press the control key and the lowercase z simultaneously), which puts you into Unix. Actually, it spawns a new Unix process. To exit the process, type the Unix command
fg

which returns you to Splus.

Splus ignores anything that you type in a line after the symbol #. Thus you can use # to include comments in your Splus code. For example,
> q() # Your grandmother wears army boots

will do exactly the same thing as


> q()

does.

Any objects that you create in Splus are (unless you specify otherwise well talk about that later) automatically stored on the disk, not in RAM. That means that when you exit Splus and return to it later, everything will still be there. In R everything is stored in RAM and only written to the disk (a le referred to as the workspace) at the end of the session. This is actually optional - you need to tell it to save the workspace when prompted. When you invoke Splus, it searches your present working directory for a subdirectory called .Data in which to store things. If none is found, it searches your home directory for a subdirectory called .Data. If none is found, it either gives an error message or creates a .Data subdirectory in your home directory. Before entering Splus, you should create a subdirectory called .Data of whatever directory you want to work in, using the Unix command mkdir. When you invoke R, it searches your present working directory for a le called .RData in which to store things. In then follow through the same process as Splus, but looking for .RData instead of .Data.

5.1 Assignment
The assignment operator is , the underscore character. You can also use a left arrow < a less-than sign followed by a hyphen.
> a2 > a<-2 # set a equal to 2 # same thing

Printing value of an object


> a > print(a) # # # # prints the value of a does the same thing - necessary if the output is directed to a file rather than the screen

Whats in the workspace (.Data)


> ls()

Value of last expression


> 7 > .Last.value # print the number 7 # 7 was stored in .Last.value

Combine into a vector


> a <- c(1,2,9.8) # create a vector of length 3

Consecutive integers
> a <- 2:6 # creates a vector (2,3,4,5,6)

Arithmetic
> > > > > > > > b <- 2*a+1 # multiply each element of a by 2 and add 1 b <- a/2 # division b <- a3.1 # raise to power 3.1 b <- log(a) # natural log b <- log10(a) # base 10 log b <- log(a,base=2) # base 2 log b <- log(a,2) # same thing mean(a) # mean of the elements of a

5.2 Help
> > > > > help(log) ?help help() help(help) args(log) # # # # # # help on the function log() same thing general help (i.e. help on help()) same thing shorter version of help(log) giving only the arguments to log()

When in help, a < ret > (i.e. a carriage return) scrolls down one line, d scrolls down one page, u scrolls up one page, and q quits.
> help.start() # open an interactive help window

5.3 Random numbers


> x <- rnorm(100) # generate 100 standard normal variates > y <- rchisq(100,2) # 100 chisquares with 2 df > set.seed(101) # sets the random number generator seed

5.4 Simple graphics


> motif() > help(Devices) > hist(x) > plot(x,y) # # # # # open a motif graphics window Splus only info on other graphics devices histogram of x scatter plot of x versus y

You can print the current graphics screen by selecting the print option with your mouse.
> dev.off() # turn off graphics device

5.5 Input/output choices


> source("example.s") # executes all commands in a file

If theres a mistake in the le and a command cannot be executed, the whole process is aborted and the workspace is restored to the state it was in before you issued the source() command.
> sink("example.out") > sink() # directs output to a file # directs output to the screen again

5.6 Batch mode


From Unix, the command Splus BATCH commands.s commands.out will execute in batch mode all the commands in the le commands.s and will direct the output to a le called commands.out. While it is executing, you may log out if you wish. Also, you can monitor the progress of the batch job by looking at the output le with the Unix commands cat, more, or less. The same is try for R, but it is called as:
R BATCH commands.s commands.out

5.7 Command line editing in Splus


If you invoke Splus from Unix by typing Splus -e you will be able to use the command-line editing feature. Your Splus session will behave like a session in your favorite editor, as long as that editor is either vi or Emacs. The most commonly used editor keystrokes (beginning of line, end of line, up one line, etc.) will then operate on your commands. The default editor is vi. To change it to Emacs, type
> options(editor="emacs")

at the beginning of your Splus session. To make Emacs the editor for all your future Splus sessions, type:
> .First <- function() options(editor="emacs")

Then the command options(editor=emacs) will automatically execute every time you enter Splus. The same hold for R but it is automatically invoked at startup.