Simple arithmetic may be done directly, including simple expressions like:

3 + 4or more complicated expressions like:

3.7 + 11 * exp(2) - 17^2 + sqrt(5) / 3Variables may be assigned by typing "=", e.g. to assign the variable "w" the value 7.2, type:

w = 7.2Alternatively, the "=" may be replaced by "<-" (i.e. "less-than" followed by "dash"), as in "w <- 7.2".

Lists of data may be entered directly using the "c( )" function, for example:

x = c(2, 4, 1.7, -3)Once this is done, then typing just "x" will output x as a list (i.e. vector). Or, typing e.g. "x[3]" will output the 3rd element, 1.7. (Also "length(x)" is 4, so e.g. "x[length(x)]" will give -3.)

Once x is a list, then its sum, mean (x-bar), variance (s^2), standard deviation (s), etc. can be computed directly:

sum(x)[Of course, "sd(x)" is the same as "sqrt(var(x))".] If you wish, you can assign these values to other variables, e.g. "mu = mean(x)", "s = sd(x)", etc.

mean(x)

var(x)

sd(x)

Lists can themselves be operated on. In the example above, "x^2" would produce the list (4, 16, 2.89, 9), so that e.g. "mean(x^2)" would give 7.9725. Equivalently, you could first do "z = x^2", and then "mean(z)" would also be 7.9725.

R also has excellent **plotting** features. For example,
"plot(x)" will plot the individual values of x above, while "hist(x)"
will display a histogram. Also, "pie(x^2)" will display a pie-chart of
x^2 (or any other non-negative list). Try them! [If you prefer, you
can save your plot as a pdf file, by typing "pdf()", then "plot(x)" (or
whatever), and then "dev.off()". If you want a jpeg or postscript
file instead of pdf, then type "jpeg()" or "postscript()" instead of "pdf()".]

R can also generate random values. For example, to generate 50 i.i.d. draws from a standard normal distribution, type "rnorm(50)". To save them as a list called "y", type "y = rnorm(50)". Then you can compute their mean etc. by "mean(y)", "var(y)", "sd(y)", etc.

To generate from other distributions, use e.g. "rbinom(50, 10, 0.3)" for the Binomial(10, 0.3) distribution, "rpois(50, 14)" for the Poisson(14), "rexp(50, 0.3)" for Exponential(0.3), etc. To compute cdf's, use e.g. "pnorm(1.2)" for the standard normal, "pexp(1.2, 0.3)" for Exponential(0.3), "pchisq(74.22, 100)" for the chisquared(100) distribution, "pt(2.228, 10)" for the t(10) distribution, "pf(5.7, 2, 10)" for the F(2, 10) distribution, etc.; for densities use "dnorm(1.2)", "dexp(1.2, 0.3)", etc.

Once you have typed "y = rnorm(50)", then you can e.g. get a histogram of this sample by typing

hist(y)To then e.g. get a second histogram, of a fresh sample, overlaid on the first, in a different colour, you could type

hist(rnorm(50), add=TRUE, border=2)

Longer sequences of R commands (e.g. full computer programs) can be saved to a file, and then executed by typing source("filename").

The number sign # indicates a COMMENT (useful for explaining what you are doing); everything from # to the end of the line is ignored by R.

When typing commands in R, you can use the up-arrow key to retrieve previous commands, and the left-arrow key to edit a command that you've already typed.

R has lots of built-in help available. For example, to find out more about the "hist" function, simply type "help(hist)" or "?hist" after the R prompt. Or, for an interactive R-help web interface, type "help.start()".

R is a full-featured programming language, and has a huge number of features and options not mentioned here. There is lots of documentation about R available on the web, see e.g. here or here or here or here or here.

When you are done with your computations, type "q()" to quit R.

[Return to STA410/2102 main page.]