This is an old revision of the document!


Examining Data

The Dollar Sign

One of the most useful things to know in R is that the dollar sign, $, lets you access variables within a data set. For example, if you’re looking at the dataset called labike, you might want to access the variable bike_count_pm to make a plot, to calculate the average, etc. To tell R you want that variable, use this syntax:

labike$bike_count_pm

## [1] 62 48 216 72 58 150 63 353 53 121 93 119 110 65 93 977 105
##[18] 56 164 97 35 39 97 73 42 103 153 138 145 35 118 90 391 52
##[35]220 155 49 40

What are these numbers? Well, R is printing out every value in that column of the dataset. Looking at Figure 18, you can see that the first value in that column is 62, and that is the first number listed by R, above. R prints values from left to right, and when it runs out of room on a line it starts over on the next line. La Bike Data

The numbers along the side tell you the index of that data point. So, 62 is the first data point, because it appears next to the [1], and 56 is the 18th data point, because it appears next to the [18].

mean(labike$bike_count_pm)

## [1] 132.9
In this example, we are taking the mean of the variable bike_count_pm, and it is 132.9. Because this is only one number, the index [1] appears next to it.

Summaries of data (“Frequency tables”)

If you want to know some basic frequencies or statistics about a particular variable, summary() is a very useful command.

summary(labike$type)

So, for the variable type, the summary() command tells us how many responses there were in each of those categories.

summary(labike$bike_count_pm)

If you use summary() on a variable that is numeric, like bike_count_pm, it gives us some basic statistics, like the minimum and maximum values, the median and mean, and the 1st and 3rd quartiles.

Sorting and ordering

Because R is a statistical programming language, not a spreadsheet program like Excel, it isn’t as natural to sort a data set. If you think about it, why do we like sorting datasets? Well, it allows us to know the maximum and minimum value of a variable. But, summary() will tell us the maximum and minimum values. Sorting also allows us to scroll through and see the distribution of values, but it’s hard to hold enough information in our head to really grasp the distribution from scrolling (people can usually only remember 7 numbers at once, which is why we have 7-digit phone numbers!). A better way to see a distribution of values would be making a histogram, hist().

With that said, if you want to sort your dataset, there is a way to do it. We use the order() command. But, if we use the command alone, it doesn’t give us what we want:

order(labike$bike_count_pm)

What are these values? Well, look through the data in the viewing pane– what row has the smallest number in the bike_count_pm column? It’s a tie between row [21] and row [30], which both have 35 in that column. Notice that the first two numbers that R printed out were 21 and 30, so it’s just giving us the list of indices, ordered by the values in bike_count_pm.

So to get the results we really want, we need to apply it to the dataset

labike[order(labike$bike_count_pm),]

This is what we wanted, right? Our Console window isn’t wide enough to see all the columns at once, so R is printing out the last two columns after the rest of the dataset, but we can see that the data is sorted by bike_count_pm.

But, this is just printing the values out in the Console window. If you look up at your file-viewing pane, the labike data has not changed order. Again, this is because R is a programming language, not a spreadsheet program. If we want the data in the viewing pane to change, we need to save over the old version of labike, like so

Determining data types

R has a few useful functions for learning about variables. One is class(),

A similar function that provides a little more information is attributes(),

Notice that class(labike$latitude) told us something about the latitude variable, but that attributes(labike$latitude returned a NULL response. So, attributes() isn’t always the better choice.

Tables (contingency tables)

To get summaries of the number of variables in a given category (or set of categories), table() is a great command. If you run, you get a table of values (sometimes called a contingency table)

Notice that providing two variables as arguments resulted in a two-way contingency table.

Descriptive statistics

We’ve discussed summary() before, if you run this on a numeric variable you get a fair number of descriptive statistics.

Notice the only tricky thing here– including na.rm=TRUE. If you don’t include that option, all these functions will return NA, because they are trying to compute a number and are encountering NA values. By passing na.rm=TRUE, you are telling the function to remove na values, thus na.rm.

Transforming data

One of the great things about R is that it acts like a calculator, and it can be used to transform data very easily. For example, say you wanted to transform the height variable in the cdc data set from height in centimeters to height in inches. We know the formula is inches = centimeters × 39.37, and that’s easy to do in R,

Print/export