An R Tutorial Starting Out
An R Tutorial Starting Out
An R Tutorial Starting Out
1. Starting Out
Once you enter R, you will receive the following prompt: >
To exit R, issue the following command:
> q()
Note: You should always type your code in a separate text file. (Notepad is preferable
to Word because it won’t try to auto-correct your code.) You can cut and paste from
notepad into R and keep a copy of everything you’ve done in the text file. It is good
practice to put your data, written code, and .RData file all in the same folder (e.g.
“Statistics”.)
2. Basics
# Here are some illustrations of R’s interactive nature:
> 5 # you type in a 5 at the prompt (note that anything on a line following
# a ‘#’ sign is considered a comment and will not be processed)
[1] 5 # → here a 5 is returned
# To view x:
> x
[1] 3 4 7
# The last two arguments of the matrix function are the number of rows and columns,
# respectively. If you would like the numbers to be entered by row instead of the default
# by column, add an additional argument, byrow=T.
# matrix(vector of data, numrows, numcols, byrow=T)
# You can create your own functions using the following format:
> newfunc <- function(args)
{
commands
}
> x[-3] # this will provide all numbers in a vector except for the third
[1] 3 4
> x[x >=5] # this will produce all numbers in x that are greater than or equal to 5
[1] 7
# Notice that arithmetic operations are applied to each element in a vector. For example:
> x+3
[1] 6 7 10
4. Distributional Functions
# Determine the area to the left of 0 for a normal (0,1) random variable:
> pnorm(0) # for the normal dist, the default mean is 0 and sd is 1
[1] 0.5
# Functions for other distributions work in a similar way. Remember that any manual
# can be accessed using the help( ) function. For example, if you want the syntax for
# the t distribution function, try help(rt)
5. Plotting Data
# R has some very nice graphics capabilities. We’ll only cover a minimal amount of
# information here.
# Here, the first column is the explanatory variable and the second column is the
# response variable.
# To plot the data points and have the points connected by lines:
> plot(regdata[,1], regdata[,2], type=‘b’)
# Enter ‘help(par)’ to see the many options available for dealing with graphics. Many
# of the options (e.g., xlim, ylim) can be used directly with the plotting functions.
8. Useful References
Dalgaard P. (2002). Introductory Statistics with R. New York: Springer-Verlag.
Ripley B.D., Venables W.N. (2002). Modern Applied Statistics with S (4ed). New
York: Springer-Verlag.
www.r-project.org
http://cran.r-project.org/doc/contrib/refcard.pdf