Introduction to Statistics (MAT/SST 115.03 2008S)

R notes for Activity 10-3


We'll be learning a bit more of R in this exercise. In particular, we'll think about ways to create new columns in a data frame and revisit ways to select rows from a data frame.

You can read in the data for this exercise with

ICC = read.csv("/home/rebelsky/Stats115/Data/IceCreamCalories.csv")

You can see the first and last few lines of the data set with

head(ICC)
tail(ICC)

As those lines suggest, there are six columns in the table BenAndJerrys, BJcal, ColdStoneCreamery, CScal, Dreyers, and Dcal. You may also note that there are a lot of NA values at the end of the table. That's because the different columns are of different lengths, but R likes to pad them into uniform lengths.

Exercise a: Five-number Summaries

Recall that you can get a five-number summary using the summary function.

summary(ICC$BJcal)
summary(ICC$CScal)
summary(ICC$Dcal)

It is also possible to get all the summaries at once by just asking for a summary of the frame.

summary(ICC)

b. Boxplots

As you might hope, you make box plots in R with the boxplot command. You make the simplest box plots from vectors.

boxplot(ICC$BJcal)

Suprisingly, R likes to make vertical box plots, rather than the horizontal box plots that most of us like. To make R make horizontal boxplots, you add horizontal=T to the command.

boxplot(ICC$BJcal, horizontal=T)

Of course, we often like to stack box plots on top of each other to compare variables (as this problem requests). If the variables are already in a frame, we can just use those columns of the frame.

boxplot(ICC[,c(2,4,6)], horizontal=T)

f. Converting Calories

This exercise asks you to create a new column in the table. You can create a new column in a table by referring to it. If the column is based on other columns, you use the appropriate formula to compute it. For example, suppose we call the new column CScal2

ICC$CScal2 = ICC$CScal / 170 * 146 / 2

h. Comparing Calories, Revisited

Recall that we made a box plot from columns 2, 4, and 6 of the frame with

boxplot(ICC[,c(2,4,6)], horizontal=T)

For the new box plot, you want columns 2, 7, and 6. (Or, if you want to keep the old data, 2, 4, 7, and 6.)

Creative Commons License

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright (c) 2007-8 Samuel A. Rebelsky.

This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/2.5/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.