R Workshop: Some R basics
- To quit R, use q().
- R is case-sensitive.
This means that abc, ABC and aBc
are different variables in R.
- Round brackets are used for function arguments;
square brackets are used for indexing vectors and matrices.
- Functions calls need to use () after them,
even if there are no arguments to pass
(see q() above).
- The underscore character _ cannot be used in variable names;
it is common in R to use the period;
for example,
model1.residual is a valid variable name,
but model1_resid is not a valid variable name.
Numbers can be used in variable names, except as the first character.
The following are all valid variable names:
x3, x2.2, D.2.x.2
- To assign variables, use <- though
later versions also allow = to be used.
The use of <- is preferred at this stage.
For example, use x <- 3 as an assignment.
Other alternatives:
3 -> x (OK)
x _ 3 (This is valid, but use this and you'll be lynched; it's very hard to read.)
- Strings are given in double quotes not single quotes;
for example: fred <- "Title for my plot"
- Lines continue to the next automatically if they are unfinished.
- Help is found using ?command or help("command")
To use the html help,
first type help.start().
To search for help,
try help.search("command").
- A list of the current variables can be found using
objects() or ls().
It is good practice to remove unwanted variables using rm(variable.name)
- When calling functions,
the names of the input variable can be used explicitly
(this is unlike MATLAB).
For example,
plot(x, y, xlab="The x-axis label")
explicitly assigns a value to xlab
even though it appears late in the function definition.
Return to main page