Loading the data

library(curl)
library(ggplot2)
library(plyr)
gapminder_location<-curl(url = "https://raw.githubusercontent.com/resbaz/r-novice-gapminder-files/master/data/gapminder-FiveYearData.csv")
gapminder<-read.csv(gapminder_location)

If you’re curious about where this data comes from you might like to look at the Gapminder website.

As its file extension would suggest, the file contains comma-separated values, and seems to contain a header row. We use read.csv to read the data into R. You can also use this function to load csv files on your computer.

Unlike bash scripts RStudio offers you great flexibility in running code from within the editor window. To run a line of code from a script click on the line and click on the Run button just above the editor panel, or hit Ctrl-Enter in Windows / Linux or Command-Enter on OS X. To run a block of code, select it and then Run.

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 clicking “gapminder” in the upper right-hand pane of RStudio.

Examining the data

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  : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##  $ pop      : num  8425333 9240934 10267083 11537966 13079460 ...
##  $ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
##  $ 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
## [1] 1952 1957 1962 1967 1972 1977

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.

gapminder_location<-curl(url = "https://raw.githubusercontent.com/resbaz/r-novice-gapminder-files/master/data/gapminder-FiveYearData.csv")
gapminder <- gapminder<-read.csv(gapminder_location,stringsAsFactors = FALSE)

Remember, if you do turn 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. If you change R’s default options, programs written by others may not run correctly in your environment and vice versa. For this reason, some argue that most such changes should be in your scripts, where they are visible.

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" ...