Programming with R
Reading in data
Learning Objectives
- Read tabular data from a file into a program.
- Assign values to variables.
Download the gapminder data from here.
- Download the file (CTRL + S, right mouse click -> “Save as”, or File -> “Save page as”)
- Make sure it’s saved under the name
gapminder-FiveYearData.csv
If you’re curious about where this data comes from you might like to look at the Gapminder website.
Now we want to load the gapminder data into R.
As its file extension would suggest, the file contains comma-separated values, and seems to contain a header row.
We can use read.csv
to read this into R.
gapminder <- read.csv(file="gapminder-FiveYearData.csv")
As in bash, we can use head to view the first few lines of the file.
head(gapminder)
country year pop continent lifeExp gdpPercap
1 Afghanistan 1952 8425333 Asia 28.801 779.4453
2 Afghanistan 1957 9240934 Asia 30.332 820.8530
3 Afghanistan 1962 10267083 Asia 31.997 853.1007
4 Afghanistan 1967 11537966 Asia 34.020 836.1971
5 Afghanistan 1972 13079460 Asia 36.088 739.9811
6 Afghanistan 1977 14880372 Asia 38.438 786.1134
Alternatively, we can view the file by doubleclicking “gapminder” in the upper right-hand pane of RStudio.
Examining the dataset
Let’s have a look at our example data (life expectancy in various countries for various years).
There are a few functions we can use to interrogate data structures in R. Let’s use them to explore the gapminder dataset.
The first thing you should do when reading data in, is check that it matches what you expect, even if the command ran without warnings or errors. The str
function, short for “structure”, is really useful for this:
str(gapminder)
'data.frame': 1704 obs. of 6 variables:
$ country : chr "Afghanistan" "Afghanistan" "Afghanistan" "Afghanistan" ...
$ year : int 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
$ pop : num 8425333 9240934 10267083 11537966 13079460 ...
$ continent: chr "Asia" "Asia" "Asia" "Asia" ...
$ lifeExp : num 28.8 30.3 32 34 36.1 ...
$ gdpPercap: num 779 821 853 836 740 ...
We can see that the object is a data.frame
with 1,704 observations (rows), and 6 variables (columns). Below that, we see the name of each column, followed by a “:”, followed by the type of variable in that column, along with the first few entries.
The class
function will show us the type of data structure (a data frame) without additional information.
class(gapminder)
[1] "data.frame"
The typeof
function shows us the type of data in the variable.
typeof(gapminder)
[1] "list"
Here we see that data frames are lists ‘under the hood’.
The gapminder data is stored in a “data.frame”. This is the default data structure when you read in data; it is useful for storing data with mixed types of columns.
Let’s look at some of the columns individually. We can access each column using the name of the dataset followed by a $ and then the name of the column. For example:
gapminder$year
We can also look at the characteristics of the data in the column:
typeof(gapminder$year)
[1] "integer"
typeof(gapminder$lifeExp)
[1] "double"
A “double” is a decimal value.
Can anyone guess what we should expect the type of the continent column to be?
typeof(gapminder$continent)
[1] "integer"
If you were expecting a the answer to be “character”, you would rightly be surprised by the answer. Let’s take a look at the class of this column:
class(gapminder$continent)
[1] "factor"
One of the default behaviours of R is to treat any text columns as “factors” when reading in data. The reason for this is that text columns often represent categorical data, which need to be factors to be handled appropriately by the statistical modeling functions in R.
However it’s not obvious behaviour, and something that trips many people up. We can disable this behaviour and read in the data again.
options(stringsAsFactors=FALSE)
gapminder <- read.csv(file="data/gapminder-FiveYearData.csv")
Remember, if you do turn it off automatic conversion to factors you will need to explicitly convert the variables into factors when running statistical models. This can be useful, because it forces you to think about the question you’re asking, and makes it easier to specify the ordering of the categories.
However there are many in the R community who find it more sensible to leave this as the default behaviour.
We can view the column names of the data.frame using the function colnames
:
colnames(gapminder)
[1] "country" "year" "pop" "continent" "lifeExp" "gdpPercap"
You can also change the column names this way (use a copy of the data rather than changing the column names of the original data).
copy <- gapminder
colnames(copy) <- letters[1:6]
head(copy, n=3)
a b c d e f
1 Afghanistan 1952 8425333 Asia 28.801 779.4453
2 Afghanistan 1957 9240934 Asia 30.332 820.8530
3 Afghanistan 1962 10267083 Asia 31.997 853.1007
Similarly, we can view the row names of the data.frame using the function rownames
:
rownames(gapminder)[1:20]
[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14"
[15] "15" "16" "17" "18" "19" "20"
See those numbers in the square brackets on the left? That tells you the number of the first entry in that row of output. So we see that for the 5th row, the rowname is “5”. In this case, the rownames are simply the row numbers.
There are a few related ways of retrieving and modifying this information. attributes
will give you both the row and column names, along with the class information, while dimnames
will give you just the rownames and column names.
In both cases, the output object is stored in a list
:
str(dimnames(gapminder))
List of 2
$ : chr [1:1704] "1" "2" "3" "4" ...
$ : chr [1:6] "country" "year" "pop" "continent" ...