Data can have different “types”. The core data types in R are
“logical” (true/false), “integer” (-1, 0, 1, etc), “numeric”/“double” (floating point numbers, a.k.a. decimals or real numbers),
and “character” (strings). There is also “complex” type for
complex numbers, but you have to go out of your way to find them.
A logical vector:
1
c(TRUE,FALSE)
1
## [1] TRUE FALSE
An integer vector: (you rarely need to specify these
explicitly).
1
c(1L,2L)
1
## [1] 1 2
A numeric vector:
1
c(1,2)
1
## [1] 1 2
A character vector:
1
c("string1","string 2 with spaces")
1
## [1] "string1" "string 2 with spaces"
(even though they are called “characters”, there is no special type
for single letters, etc. So people will refer to these as strings
and characters fairly interchangeably).
Note that the strings must be quoted. You can use single or
double quotes as you fancy, but be consistent. The difference
between integer and numeric is subtle and can usually be ignored.
To force numbers to be integers, you need to specify them with an
“L” after, as above. Generally don’t worry about this.
It is possible to refer to TRUE and FALSE as T and F, and
this practice is fairly widespread. Do not do it. The values
TRUE and FALSE are special values are protected – they can
never be overwritten:
1
TRUE<-FALSE# invalid code, causes error
1
## Error: invalid (do_set) left-hand side to assignment
Wheras the values T and F are just variables bound to the
values TRUE and FALSE.
123
T<-FALSEF<-TRUET# is now FALSE!
1
## [1] FALSE
1
F# is now TRUE!
1
## [1] TRUE
Do this at the top of of someone’s script and watch their world
burn.
Some functions will work on any type, others will require specific
types. For example, length will give the length for any vector
1
length(c(1,2,3,4))
1
## [1] 4
1
length(c("a","b","c","d"))
1
## [1] 4
max will give the maximum value for a vector:
1
max(c(1,2,3,4))
1
## [1] 4
1
max(c("a","b","c","d"))
1
## [1] "d"
but sum requires a vector that is not a character vector:
1
sum(c(1,2,3,4))
1
## [1] 10
1
sum(c("a","b","c","d"))
1
## Error: invalid 'type' (character) of argument
How do you tell which of these types you have? Use the typeof
function:
1
typeof(c(TRUE,FALSE))
1
## [1] "logical"
1
typeof(c(1L,2L))
1
## [1] "integer"
1
typeof(c(1,2))
1
## [1] "double"
1
typeof(c("a","b","c"))
1
## [1] "character"
Note that the type of the number 1 is numeric:
1
typeof(1)
1
## [1] "double"
Unless you ask nicely, all values in R are numeric and not
integer. But this usually does not matter.
Note: this section actually grossly oversimplifies what is going
on; see the difference between mode, storage.mode, class and
typeof.
Container data types
In R’s terminology, the types above are “atomic data types” as they
cannot be subdivided any further. Data go into containers, which
also have types.
The simplest of these is the vector, which we’ve already seen. A
vector contains zero or more elements of a single atomic type.
So you have a vector of integers, or a vector of characters, etc.
You can’t have a mix of types within a vector.
1
c(1.5,"a")# The number 1.5 is converted into a character string
1
## [1] "1.5" "a"
Next simplest is a matrix. This is a two dimensional object with
all elements being the same type. These can be constructed with
the matrix function
12
m <- matrix(c(1,2,3,4,5,6),2,3)m
123
## [,1] [,2] [,3]## [1,] 1 3 5## [2,] 2 4 6
Notice that the elements are added column-wise (can be changed by using option byrow=TRUE in the matrix function). A matrix is simply
a vector that knows its dimension (in this case 2-by-3). You can
do element-wise operators on a matrix:
a numeric vector of length 1 containing the element 1.5
a logical vector of length 2 containing the values TRUE and
FALSE
a character vector of length 3 containing the elements “a”, “b”, and “c”.
A data.frame is actually a list internally, and many approaches
for working with lists work with data.frames.
Factors vs. strings
By default, R reads in data.frames with things that look like
text as “factors”. Often an experiment includes trials for different levels of some explanatory variable. For example, when looking at the impact of fertilizer on the growth rate of a plant you might try to observe how different plants grow when exposed to different preset concentrations of fertilizer. The different levels are also called factors. In our dataset, we have 1 variable/column Plot with factors.
12
data <- read.csv("data/seed_root_herbivores.csv")data$Plot