Teaching: 40 min
Exercises: 15 min
Questions
Objectives
create data/feline-data.csv using:
add the following data to the file:
coat,weight,likes_string
calico,2.1,1
black,5.0,0
tabby,3.2,1
cats <- read.csv(file = "~/Desktop/gps_r/feline-data.csv")
cats
he read.csv function is used for reading in tabular data stored in a text file where the columns of data are delimited by commas (csv = comma separated values).
Tabs are also commonly used to separated columns - if your data are in this format you can use the function read.delim.
There is also the general read.table function that is used if the columns in your data are delimited by a character other that commas or tabs.
With data loaded, we can now explore our data set, pull out columns and specify the musing the $ operator:
cats$weight
cats$coat
We can do other operations on the columns:
## say we discovered that the scale weights two Kg light:
cats$weight + 2
paste("my cat is", cats$coat)
But what about if we type this:
cats$weight + cats$coat
The last command we ran returned an error because
data types
typeof(cats$weight)
Here are a few more examples of checking the data type:
typeof(1L)
typeof(1+1i)
typeof(TRUE)
typeof('banana')
For the next example:
Load the new cats data like before, and check what type of data we find in the weight column:
cats <- read.csv(file="~/Desktop/gps_r/feline-data_v2.csv")
typeof(cats$weight)
Oh no, our weights aren’t the double type anymore!
If we try to do the same math we did on them before, we run into trouble:
cats$weight + 2
R reads a csv into a table, it insists that everything in a column be the same basic type
if it can’t understand everything in the column as a double, then nobody in the column gets to be a double
the table that R loaded our cats data into is called a data.frame and our first example of something called a data structure
a data structure is a structure that R knows how to build out of basic data types.
we can see that it is a data.frame by calling the class function on it:
class(cats)
Now, in order to successfully use our data in R, we need to understand what the basic data structures are, and how they behave.
For now, let’s remove that extra line from our cats data and reload it, while we investigate this behavior further:
in RStudio reload the feline-data.csv
cats <- read.csv(file="~/Desktop/gps_r/feline-data.csv")
To better understand this behavior, let’s meet another of the data structures: the vector.
my_vector <- vector(length = 3)
my_vector
A vector in R is essentially an ordered list of things, with the special condition:
that everything in the vector must be the same basic data type.
If you don’t choose the datatype, it’ll default to logical; or, you can declare an empty vector of whatever type you like.
another_vector <- vector(mode='character', length=3)
another_vector
You can check if something is a vector:
str(another_vector)
The cryptic output from this command indicates the basic data type found in this vector -
If we similarly do:
str(cats$weight)
we see that that’s a vector, too - the columns of data we load into R data.frames are all vectors,
if you can interpret one entry in the column as a number, then you can interpret all of them as numbers, so we don’t have to check every time.
This consistency, like consistently using the same separator in our data files, is what people mean when they talk about clean data; in the long run, strict consistency goes a long way to making our lives easier in R
You can also make vectors with explicit contents with the combine function:
combine_vector <- c(2,6,3)
combine_vector
Thinking about what we have covered so far, what do you thing the following will produce?
quiz_vector <- c(2,6,'3')
str(quiz_vector)
This is something called type coercion, and it is the source of many surprises and the reason why we need to be aware of the basic data types and how R will interpret them.
When R encounters a mix of types (here numeric and character) to be combined into a single vector, it will force them all to be the same type.
Now Consider these examples:
coercion_vector <- c('a', TRUE)
coercion_vector
another_coercion_vector <- c(0, TRUE)
another_coercion_vector
The coercion rules go: ** logical -> integer -> numeric -> complex -> character,
where -> can be read as and transformed into.
character_vector_example <- c('0','2','4')
character_vector_example
character_coerced_to_numeric <- as.numeric(character_vector_example)
character_coerced_to_numeric
numeric_coerced_to_logical <- as.logical(character_coerced_to_numeric)
numeric_coerced_to_logical
As you can see, some surprising things can happen when R forces one basic data type into another!
Nitty-gritty of type coercion aside, the point is: if your data doesn’t look like what you thought it was going to look like, type coercion may well be to blame;
make sure everything is the same type in your vectors and your columns of data.frames, or you will get nasty surprises!
But coercion can also be very useful!
For example, in our cats data likes_string is numeric, but we know that the 1s and 0s actually represent TRUE and FALSE (a common way of representing them).
We should use the logical datatype here, which has two states: TRUE or FALSE, which is exactly what our data represents.
We can ‘coerce’ this column to be logical by using the as.logical function:
cats$likes_string
cats$likes_string <- as.logical(cats$likes_string)
cats$likes_string
Combine c() or concatenate will also append things to an existing vector:
ab_vector <- c('a', 'b')
ab_vector
combine_example <- c(ab_vector, 'SWC')
combine_example
You can also make series of numbers:
mySeries <- 1:10
mySeries
seq(10)
seq(1,10, by=0.1)
We can ask a few questions about vectors:
sequence_example <- seq(10)
head(sequence_example, n=2)
tail(sequence_example, n=4)
length(sequence_example)
class(sequence_example)
typeof(sequence_example)
Finally, you can give names to elements in your vector:
names_example <- 5:8
names(names_example) <- c("a", "b", "c", "d")
names_example
names(names_example)
(hint: there is a built in vector called LETTERS)
x <- 1:26
x <- x * 2
names(x) <- LETTERS
Now we are going to briefly cover data frames. Previously, We said that columns in data.frames were vectors:
str(cats$weight)
str(cats$likes_string)
These make sense. But what about:
str(cats$coat)
Another important data structure is called a factor.
Factors usually look like character data, but are typically used to represent categorical information.
For example, let’s make a vector of strings labelling cat colorations for all the cats in our study:
coats <- c('tabby', 'tortoiseshell', 'tortoiseshell', 'black', 'tabby')
coats
str(coats)
We can turn a vector into a factor like so:
CATegories <- factor(coats)
class(CATegories)
str(CATegories)
Now R has noticed that there are three possible categories in our data - but it also did something surprising;
instead of printing out the strings we gave it, we got a bunch of numbers instead.
R has replaced our human-readable categories with numbered indices under the hood:
typeof(coats)
typeof(CATegories)
One solution is use the argument stringAsFactors:
cats <- read.csv(file="~/Desktop/gps_r/feline-data.csv", stringsAsFactors=FALSE)
str(cats$coat)
Another solution is use the argument colClasses that allow finer control.
cats <- read.csv(file="~/Desktop/gps_r/feline-data.csv", colClasses=c(NA, NA, "character"))
str(cats$coat)
Note: new students find the help files difficult to understand; make sure to let them know that this is typical, and encourage them to take their best guess based on semantic meaning, even if they aren’t sure.
In modelling functions, it’s important to know what the baseline levels are.
This is assumed to be the first factor, but by default factors are labelled in alphabetical order.
You can change this by specifying the levels:
mydata <- c("case", "control", "control", "case")
factor_ordering_example <- factor(mydata, levels = c("control", "case"))
str(factor_ordering_example)
In this case, we’ve explicitly told R that “control” should represented by 1, and “case” by 2.
This designation can be very important for interpreting the results of statistical models!
Another data structure you’ll want in your bag of tricks is the list.
A list is simpler in some ways than the other types, because you can put anything you want in it:
list_example <- list(1, "a", TRUE, 1+4i)
list_example
another_list <- list(title = "Research Bazaar", numbers = 1:10, data = TRUE )
another_list
We can now understand something a bit surprising in our data.frame
what happens if we run:
typeof(cats)
We see that data.frames look like lists ‘under the hood’
In other words, a data.frame is a special list in which all the vectors must have the same length.
In our cats example, we have an integer, a double and a logical variable.
As we have seen already, each column of data.frame is a vector.
cats$coat
cats[,1]
typeof(cats[,1])
str(cats[,1])
Each row is an observation of different variables, itself a data.frame, and thus can be composed of element of different types.
cats <- read.csv(file="~/Desktop/gps_r/feline-data.csv")
cats$likes_string <- as.logical(cats$likes_string)
cats$likes_string
continue lesson
cats[1,]
str(cats[1,])
Try out these examples and explain what is returned by each one.
Hint: Use the function typeof() to examine what is returned in each case.
Last but not least is the matrix.
We can declare a matrix full of zeros:
matrix_example <- matrix(0, ncol=6, nrow=3)
matrix_example
And similar to other data structures, we can ask things about our matrix:
class(matrix_example)
typeof(matrix_example)
str(matrix_example)
dim(matrix_example)
nrow(matrix_example)
ncol(matrix_example)