You are on page 1of 2

# R Help Sheet 2: The R Language

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:

## BIO2426 Analysis of Biological Data Page 1 of 2

Here, mean, sum and length are all functions, while weight is the argument. Several arguments can
be included in many functions; for example, typing mean(weight, na.rm=T) will remove any missing
weight values before taking the mean (see Help Sheets 3 and 4).

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:

## • numeric – contain lists of numbers (e.g. weight)

• integer - whole numbers (1,2,3,4…) only (e.g. a count of individuals)
• logical – values must be either TRUE or FALSE (e.g. the presence or absence of a species)
• factor – categorical variables (e.g. group from the PlantGrowth dataframe).

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: