Objects in R

Types of object

Type Description
Atomic element Single value
Vector One-dimensional sequence of elements
Matrix Two-dimensional - all elements (rows and columns) are the same type
Data frame Spreadsheet - different columns can contain different types of data
Array Multi-dimensional
List Contains multiple objects- any combination of the above

The different types of objects have different structures and properties

Atomic elements

Atomic elements are single pieces of information, and can have a variety of formats:

Format Description Example
Integer Number without decimals 12
Numeric Number with decimals 3.85
Character Text “Golden cat”
POSIX date/time Any combination of time & date information 19:08 14-Apr-2013
Logical True or false TRUE
Missing information Data are not available NA

Use the class() function to find out what type of object you’re working with

pi        # The built-in pi object
[1] 3.141593
class(pi) # What type of object is pi?
[1] "numeric"

Vectors

  • Vectors contain an ordered sequence of atomic elements
  • The values themselves can be text, numeric or other kinds of data
  • All values within a single vector must be the same data type
  • A vector only has one dimension: i.e. its length, or the number of values it contains

Tip

Think of a vector as a single column of data, which can be one or many rows long

Vectors can have the same format as atomic elements, or more complex higher-order formats, including:

  • Factor: a character variable with different levels, for example country names
  • Ordered factor: the order of factor levels has meaning, for example size classes

Matrices

Matrices (singular = matrix) properties:

  • Store multiple columns (vectors)
  • Can contain only a single data type i.e. all integer or all numeric
  • Gridded geographic data are often stored as matrices, for example a two-dimensional grid of altitude measurements

Data frames

Data frames contain more complex data, including variables of various different types:

  • Two dimensional, like a spreadsheet with multiple columns (vectors) & rows
  • Adjacent columns can store different types of data, e.g. Site, Altitude, Vegetation type
  • All vectors (columns) are usually the same length
  • Width = number of variables ~ columns
  • Length = number of observations ~ rows
  • Very versatile

Lists

Lists are more complex composite objects:

  • A collection of objects such as vectors and data frames which are stored as a ‘family’
  • Component objects can be different types
  • A common example of a list is R statistical output, with
    • Model formula as a character atomic element
    • P-value as a numeric atomic element
    • Fitted values as a vector

Built-in objects

R contains some built-in objects which can be useful for creating datasets or adding information to field data

Let’s take a look at some of them - what type of object are they?:

  • letters
  • LETTERS
  • month.name
  • month.abb

Create objects

We create objects in R with the assignment operator <-, which looks like an arrow pointing left

What the arrow <- does here is assign that value to the object, in this case called x:

x <- 5*2 

Assign values to objects

You could use an equals sign = instead, but it’s recommended to use the arrow. This is for two reasons:

  1. To avoid confusion between creating an object and assigning the value of an argument within a function (more on that later)
  2. To make the direction of the assignment clear

Warning

Note that if you do use =, it has an implicit direction:

  • x = y is not the same as y = x
  • x = y means that the value of y is assigned to x
  • y -> x also means that the value of y is assigned to x (note arrow direction)

The standard approach is to put the name of the new object first, and use a left-pointing arrow

More complex objects

Storing information as objects is not limited to a single number. For example, you can store a list of numbers (or text) as a vector, for example 2,4,6,8,10

This vector has an order; the vector 10,8,6,4,2 is not equivalent

The function c() means concatenate, and glues values together into a single list:

v <- c(1,2,3,4,5)
v
[1] 1 2 3 4 5

Perform calculations on objects

You can use your objects in calculations, instead of numbers, for example:

x <- 2^3
x
[1] 8

When you include your vector v in a calculation, R performs the same calculation on every value:

vv <- v*2
vv
[1]  2  4  6  8 10

Character (Text) objects

You can also create and use text values in R

site <- "gabon"

View your objects

You’re beginning to accumulate objects in R now. Let’s remind ourselves what they all are with the list function ls():

ls() # List the objects in a workspace
[1] "site" "v"    "vv"   "x"   

To delete objects you no longer need, use the remove function rm():

rm(x) # Delete an object
ls() # What objects are left?
[1] "site" "v"    "vv"  

Delete all objects

If you want to completely clear your workspace by removing all objects, you can use the rm() function and define which objects to remove by supplying R with a list of all the objects in your workspace

Use with caution!

Make sure you have saved the code you used to create the objects if you will need them again in the future

rm(list = ls()) # Remove ALL objects (USE WITH CAUTION!)