4  Data objects

Everything you store in R is an object: a single number, a table, or the result of a statistical test. Everything you do with these objects is a function call.

Objects differ in two ways:

Many functions only work on certain types and structures. A mean of text makes no sense, for example. So when something does not work, the first thing to check is what kind of object you have.

4.1 Data types

These are the data types you will use most:

Type Example What it holds
character "hello", "low_P" text, always written in quotes
numeric 3, 14.5 numbers
logical TRUE, FALSE yes/no values, for example the result of a comparison

Numbers come in two variants: double (any number, with or without decimals) and integer (whole numbers, written with an L, like 2L). R uses doubles unless you ask for integers, and for most work you do not need to care about the difference. Both count as numeric. R knows two more data types, complex (1+4i) and raw, but you will rarely meet them in biology.

Two functions help you find out what you have:

  • class() tells you what kind of object you have
  • length() tells you how many values it holds
#create some objects
character_object <- "dataset"
number_object <- c(1,4,5)

#asking with what type we work
class(character_object)
[1] "character"
class(number_object)
[1] "numeric"
#ask how long our objects are
length(character_object)
[1] 1
length(number_object)
[1] 3

4.2 Data structures

The data structures you will meet most often are:

  • vectors
  • factors
  • matrices
  • lists
  • data frames

You do not have to tell R in advance what an object will hold. When you assign something with <-, R picks the data type and structure for you.

4.2.1 Vectors

A vector is a row of values of the same type, for example only numbers or only text. If you mix numbers and text in one vector, R turns the numbers into text.

#lets create a random vector
a_vector <- c(2, 3, 5, 7, 1) 

#show the vector we just created
a_vector
[1] 2 3 5 7 1
#asking how long your vector is
length(a_vector)
[1] 5

4.2.1.1 Vector indexing

If we want to only retrieve part of the data stored in a vector we can create a subset using the index as shown below.

  • square brackets [] = allow us to retrieve certain elements of a vector, i.e. [3] retrieves the 3rd element
  • we can combine c() and [] if we want to retrieve several elements of a vector.
#retrieve the third element stored in a vector
a_vector[3]
[1] 5
#retrieve the 1st and 3rd element by combining ``c()`` and []
a_vector[c(1,3)]
[1] 2 5
#retrieve the 1-3rd element
a_vector[c(1:3)]
[1] 2 3 5
#we can also add vectors of the same length together
x <- c(1,2,3,4)
y <- c(1,2,3,4)

#and now we can combine our vectors
x + y
[1] 2 4 6 8

Be careful when you add two vectors of different lengths. R then repeats the shorter vector until it is as long as the longer one. This is called recycling:

#adding vectors of different lengths
x <- c(1,2)
y <- c(1,2,3,4)

#x is repeated: 1+1, 2+2, 1+3, 2+4
x + y
[1] 2 4 4 6

If the length of the longer vector is not a multiple of the shorter one, R still recycles, but gives a warning. A warning is not an error: you still get a result, so read warnings carefully.

#x is repeated, but does not fit: 1+1, 2+2, 1+3
x <- c(1,2)
y <- c(1,2,3)
x + y
Warning in x + y: longer object length is not a multiple of shorter object
length
[1] 2 4 4

Another way to extend vectors is:

  • append() –> Add elements to a vector.
#add another datapoint to our vector
a_vector <- append(a_vector, 13)
a_vector
[1]  2  3  5  7  1 13
#add +1 to all our four numbers
a_vector <- a_vector + 1
a_vector
[1]  3  4  6  8  2 14
#remove the first element of our vector
a_vector <- a_vector[-1]
a_vector
[1]  4  6  8  2 14

We can also give the elements of a vector names, and then use the names to get data:

#create a vector and give it names (i.e. for counts from some microbes)
x <- c(300, 410, 531)
names(x) <- c("Ecoli","Archaeoglobus","Ignicoccus")

#check how our data looks
x
        Ecoli Archaeoglobus    Ignicoccus 
          300           410           531 
#now we can retrieve part of the vector using the names
x[c("Ecoli","Ignicoccus")]
     Ecoli Ignicoccus 
       300        531 

4.2.1.2 Changing vectors

We can also change elements in our vector:

#create a vector
x <- 1:10

#change the second last positions to 5 and 9
x[9:10] <- c(5,9)

#check if this worked
x
 [1] 1 2 3 4 5 6 7 8 5 9
#we can not only add things, we can also remove this using the minus symbol
#i.e. lets remove the third element in our vector
x[-3]
[1] 1 2 4 5 6 7 8 5 9
#if we want to remove more than one thing we can use the **c()**
#lets remove elements 4 until (and including) 9
x[-c(4:9)]
[1] 1 2 3 9

4.2.1.3 Useful functions for vectors

Apart from the basic math operators, R has many functions that work on vectors. Some common ones are log(), exp(), sqrt() and the following:

Name Function
max() select the largest element
min() select the smallest element
length() give the number of elements
sum() sum all elements
mean() calculate the mean value
var() calculate the sample variance
sort() sort the elements from small to large
#create a vector
x <- c(4, 8, 1, 5)

#try some of the functions
max(x)
[1] 8
mean(x)
[1] 4.5
sort(x)
[1] 1 4 5 8

4.2.2 Matrices

A matrix is a table in which all values have the same type, usually numbers. You will not build many matrices yourself, but you will meet them in other people’s code and as the output of some functions, for example tables of counts or distances. Many functions that work on data frames also work on matrices, such as [rows, columns], dim() and head().

#make a matrix with the numbers 1 to 12 in 4 rows, filled row by row
matrix_A <- matrix(1:12, nrow = 4, byrow = TRUE)

#check how our matrix looks
matrix_A
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
[4,]   10   11   12
#the value in the 2nd row and 3rd column
matrix_A[2,3]
[1] 6
#the number of rows and columns
dim(matrix_A)
[1] 4 3

4.2.3 Lists

A list is a collection of objects that can each have a different type and length: a few numbers, some text, a whole table, or even another list. You create a list with list().

For example, here we store cell counts (numeric), the names of the microbes (character) and whether they are alive (logical) in one list:

#define our vectors
n = c(20, 30, 50) 
s = c("Ecoli", "Archaeoglobus", "Bacillus") 
b = c(TRUE, FALSE, TRUE) 

#combine the vectors in a list
our_list = list(counts=n, strain=s, status=b) 

#show our list
our_list
$counts
[1] 20 30 50

$strain
[1] "Ecoli"         "Archaeoglobus" "Bacillus"     

$status
[1]  TRUE FALSE  TRUE

You get one element out of a list with $ and its name, the same way you get one column out of a data frame:

#get the element with the name strain
our_list$strain
[1] "Ecoli"         "Archaeoglobus" "Bacillus"     

Why should you care about lists? Because the results of many functions are lists. For example, a t-test (which we use in the statistics chapter) gives you a list with the test statistic, the p-value and more. With names() you see what is inside, and with $ you take out the part you need:

#compare the root length between the two phosphate levels
result <- t.test(Rootlength ~ Nutrient, data = growth_data)

#what kind of object is this, and what is inside?
class(result)
[1] "htest"
names(result)
 [1] "statistic"   "parameter"   "p.value"     "conf.int"    "estimate"   
 [6] "null.value"  "stderr"      "alternative" "method"      "data.name"  
#take out only the p-value
result$p.value
[1] 7.324612e-68

4.2.4 Dataframes

You already know data frames from the chapter “First steps in R”: our growth data is one. A data frame is a table in which each column is a vector. All columns have the same length, and each column has one data type, but different columns can have different types. In our growth data, Nutrient is text and Rootlength is numeric. So a data frame is a bit like a list of vectors of equal length.

Everything you learned about [rows, columns] and $ in “First steps in R” works on data frames. A few more functions are useful to look at them:

#the column names
colnames(growth_data)
[1] "Nutrient"    "Condition"   "Experiment"  "Rootlength"  "FW_shoot_mg"
#the first few row names
head(rownames(growth_data))
[1] "1" "2" "3" "4" "5" "6"

When you read a table into R, the rows get the numbers 1 to 594 as row names, unless you say otherwise.

You add a new column with $ and a new column name. Here we fill all rows with the word comment:

#add a new column
growth_data$newColumn <- "comment"

#check if that worked
head(growth_data)

You will see this way of adding columns often in other people’s code. In the next chapter you learn another way, mutate().

4.3 Check the structure of our data

When you read in your own data, check how R stored it:

  • class() tells you what kind of object you have
  • str() (short for structure) shows the data type of each column and the first few values
#check what kind of data we have:
class(growth_data)
[1] "data.frame"
#check how are different parts of our data stored?
str(growth_data)
'data.frame':   594 obs. of  6 variables:
 $ Nutrient   : chr  "low_P" "low_P" "low_P" "low_P" ...
 $ Condition  : chr  "mock" "mock" "mock" "mock" ...
 $ Experiment : chr  "ex61" "ex61" "ex61" "ex61" ...
 $ Rootlength : num  40.4 38 74.7 34.4 31.9 66.5 34.8 43.4 33.9 41.9 ...
 $ FW_shoot_mg: num  6.6 6.8 8.1 8.3 8.6 8.9 9.5 9.8 10 10.2 ...
 $ newColumn  : chr  "comment" "comment" "comment" "comment" ...

We see that

  • our data is stored in a data frame
  • the columns have different data types: chr (character) and num (numeric)
  • our data contains 594 observations (rows) and 6 variables (columns): the 5 columns of our file plus the column newColumn we added above

4.4 Factors

A factor is a vector for categorical data: data that falls into a fixed set of groups, such as our nutrient levels or treatments. The possible groups are called levels. Factors matter for statistics and for plotting, for example because the order of the levels sets the order of the groups in a plot. By default, R sorts the levels alphabetically.

You make a factor with factor():

#lets make a vector
Nutrients <- c("low_P", "low_P", "high_P", "high_P")

#lets make our own simple factor
Nutrients_factor <- factor(Nutrients)

#lets compare the vector and factor we generated
Nutrients
[1] "low_P"  "low_P"  "high_P" "high_P"
Nutrients_factor
[1] low_P  low_P  high_P high_P
Levels: high_P low_P

When we check our factor, we see that R assigns one level to low_P and another level to high_P. We can also see that R sorts the levels in alphabetical order, i.e. first we have high_P and then low_P, even though in our vector low_P came first.

4.4.1 Checking the behaviour of factors

Now, lets check how factors behave.

  • levels() shows the levels of a factor
  • nlevels() shows how many levels there are
  • Factors look like text, but R stores them as whole numbers (integers), one number per level. class() shows that we have a factor, typeof() shows how R stores it:
#only print the levels
levels(Nutrients_factor)
[1] "high_P" "low_P" 
#check how many levels we have
nlevels(Nutrients_factor)
[1] 2
#what class do we have
class(Nutrients_factor)
[1] "factor"
typeof(Nutrients_factor)
[1] "integer"

4.4.2 Ordering factor levels

Often the alphabetical order is not the order you want. In a plot, you may want low_P before high_P, or the control first. With the argument levels you set the order yourself:

#check our levels
levels(Nutrients_factor)
[1] "high_P" "low_P" 
#reorder levels
Nutrients_factor_reordered <- factor(Nutrients_factor, levels = c("low_P", "high_P"))

#check our levels
levels(Nutrients_factor_reordered)
[1] "low_P"  "high_P"

4.4.3 Converting factors

Sometimes you need to turn a factor back into text. You do this with as.character(). There are similar functions for other types, such as as.numeric().

#convert our factor to a character
Nutrients_characters <- as.character(Nutrients_factor)
Nutrients_characters
[1] "low_P"  "low_P"  "high_P" "high_P"