Escolar Documentos
Profissional Documentos
Cultura Documentos
Summary
Many people find adjusting to R quite tricky at first because it’s very different to using a point-and-
click programme like Excel or SPSS. This guide provides an introduction to how R works, with help on
some of the jargon.
Objects
Typing something into R will give you an answer in the command window, but if you want to use
something several times you need to “save” it into R’s memory by assigning it as an object using the
‘gets’ arrow (<-) You can then type the name of that object (here, x) to call back the thing you saved:
Objects needn’t be single values: they can be a series of values (vectors), or a whole database of
different variables (dataframes). R has lots of built-in dataframes, so we’ll use one as an example:
type PlantGrowth into the R command window to see the PlantGrowth dataframe. We can see that
PlantGrowth contains two variables (vectors), weight and group. Using attach(PlantGrowth) allows
us to call these variables as objects:
Functions
We can now do things to all the numbers in the weight vector simply by typing in its name. For
example, simple calculations can be done using arithmetic operators like +, -, * (multiplication), or /
(division):
In this example, we’ve added 1 to every number. However, to do more complicated things, you can
use one of R’s built-in functions. To do use a function, we type the name of each function followed
by the information we want the function to use (called arguments) in brackets. For example, to see
how many different values (or elements), there are in the weight vector, we can use the length
function:
Other handy functions for calculations include: summary, signif, sd, var, sqrt, log, asin. To find out
what these do, trying using the help function (Help Sheet 3).
Classes
There are several different types (or classes) of vectors. R treats different classes of vectors in
different ways, so it’s important to make sure the vectors you’re working with are classified
correctly. The main ones are:
To find out what type of vector you have, use the class function:
It’s often worth doing this if you get an error message, to check that R’s got things right. However,
beware that you might not always get an error message, so it’s worth making sure before you start
working with any variable. For example, if we’d classified the groups in the PlantGrowth dataframe
as groups 1, 2, and 3, R would assume that group was an integer variable (continuous series of whole
numbers) rather than a factor (categorical variable), as we intended.
New variables with the correct class assignments can be assigned using the gets arrow (Help sheet 1)
The different categories, or levels of a factor can be assessed using the levels function:
The number of appearances of each category in a factor can be totalled using the table function: