Introduction to Statistics (MAT/SST 115.03 2008S)

R Notes for Topic 8: Measures of Center


R notes for Activity 8-2: Game Show Prizes

You can use the following command to make the vector of deal amounts.

DealAmounts = c(0.01, 1, 5, 10, 25, 50, 75, 100, 200, 300, 400, 500, 750,
  1000, 5000, 10000, 25000, 50000, 75000, 100000, 200000, 300000,
  400000, 500000, 750000, 1000000)

You can use mean to compute the mean of a vector and median to compute the median of a vector.

R notes for Activity 8-3: Matching Game

Reading Data

Although the book says that the data are stored in the file Matching, we've separated the data into three separate files: MonopoloyPrices.csv, SnowfallAmounts.csv, and QuizPercentages.csv. All are stored in the standard data directory for this course. Recall that you can read a file into a frame with a command like

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

You should be able to figure out the commands to read the other two data sets.

Dot Plots

We need to load an appropriate library to do dotplots, but otherwise it remains straightforward. Particularly since we do not plan to use these graphs for anything but a quick observation, we don't even need to fool with axis labels.

library(BHH2, lib="/home/rebelsky/Stats115/Packages")
dotPlot(MonopolyPrices, main="Prices of Monopoly Properties")

You might note something a bit strange about this command. Traditionally, we use dotPlot with vectors. And, if you think about it carefully, MonopolyPrices is a data frame, rather than a vector. So, why does the command work? Apparantly, R is smart enough to treat a data frame with one column as a vector, at least for this plot.

If, however, you want to create a histogram, you'll need to extract the column (which is also called MonopolyPrices).

hist(MonopolyPrices$MonopolyPrices)

Here's a fine-enough grained histogram that it replicates the dotplot.

hist(MonopolyPrices$MonopolyPrices, breaks=seq(from=0,to=500,step=25))

Numeric Measures

As you already know, you can get the mean and median separately with mean and median. You can also get them together with a host of other information by using summary.

summary(MonopolyPrices)

R notes for Activity 8-4: Rowers' Weights

The data are now stored in Rowers04.csv. You can read them in as follows:

Rowers = read.csv("/home/rebelsky/Stats115/Data/Rowers04.csv")

Refer to the instructions from a previous activity to see how to make dotplots and how to have R compute a mean and median. In each case, you want to use the vector Rowers$Weight.

This activity asks you to remove one row (corresponding to Cipollone, the coxswain) and, later, to change the weight of Schroeder. It turns out that you need different tools for the two activities. To delete rows, you use the row selection commands. For example,

Rowers = Rowers[Rowers$Name != "Cipollone",]

To change weight use the fix command.

fix(Rowers)

Remember that R pauses while you're editing the data set, so you must close the editing window before doing anything else.

R notes for Activity 8-5: Buckle Up!

This activity requires a bit more work with R than we've done in previous exercises. We start by reading in values.

SeatBeltUsage = read.csv("/home/rebelsky/Stats115/Data/SeatBeltUsage05.csv")

The first step is to separate the table into two parts.

Primary = SeatBeltUsage[SeatBeltUsage$LawType == "Primary", ]
Secondary = SeatBeltUsage[SeatBeltUsage$LawType == "Secondary", ]

You will need to extract the CompliancePercentage column from each in order to compute dot plots, means, and medians.

dotPlot(Primary$CompliancePercentage)
summary(Primary$CompliancePercentage)

It may be helpful to draw each dot plot in a separate window. In R, the way we create a new output window depends on the computer system we're using. On Linux (which we use in class), the command is X11(). On a Mac, the command is quartz(). On Microsoft windows, the command is windows(). Hence, on our Linux boxes, you might create new windows for the two new plots with.

X11()
dotPlot(Primary$CompliancePercentage)
X11()
dotPlot(Secondary$CompliancePercentage)

R notes for Activity 8-17: Marriage Ages

You can load the data for this activity with

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

Since this is a homework activity, the remaining instructions are left for you to determine.

R notes for Activity 8-18: Quiz Percentages

As you may recall from Activity 8-3, you can load the data for this activity with

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

Since this is a homework activity, the remaining instructions are left for you to determine. (Recall that you can use fix to change the data.

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.