Professional Documents
Culture Documents
Lec 5
Lec 5
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()
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
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
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
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
You can print the current graphics screen by selecting the print option with your mouse.
> dev.off() # turn off graphics device
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
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.