# R notes for Activity 10-4: Fan Cost Index

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.

## Getting Started

To read in the initial Fan Cost Index table, use

```FanCost = read.csv("/home/rebelsky/Stats115/Data/FanCost06.csv")
```

You can get a look at the first few lines of the table with

```head(FanCost)
```

As that summary suggests, the columns are `Team`, `Adult`, `Child`, `Parking`, `Program`, `Cap`, `Beer`, `Beer.Oz`, `Soda`, `Soda.Oz`, and `Hot Dog`.

## a. Computing FCI

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, if FCI were based on one adult ticket and three children's tickets, you would write

```FanCost\$FCI = FanCost\$Adult + 2*FanCost\$Child
```

You can see that the new column is added with `head`.

It is, of course, left to the reader to figure out what formula to use for the FCI reported in the book. (If you can't figure it out, it is reproduced at the end of this section.)

Next, this exercise asks you to find the team with the highest and lowest fan cost. You should start by finding out those values.

```max(FanCost\$FCI)
min(FanCost\$FCI)
```

Now, how do we figure out which teams correspond to those numbers. We can look at the data (just type `FanCost`). We can look at just the columns that correspond to the data.

```FanCost[,c(1,12)]
```

or

```data.frame(City=FanCost\$Team,FCI=FanCost\$FCI)
```

However, our best bet is to get R to search for us.

```FanCost[FanCost\$FCI == max(FanCost\$FCI), ]
FanCost[FanCost\$FCI == min(FanCost\$FCI), ]
```

## Exercise b: Plotting

I'm not sure why the book asks for a dotplot, rather than a boxplot, but, hey, we'll do both.

```library(BHH2, lib="/home/rebelsky/Stats115/Packages")
dotPlot(FanCost\$FCI)
X11()
boxplot(FanCost\$FCI, horizontal=T)
```

## Exercise h: Price Per Ounce

Just in case it wasn't clear, you can use a similar technique for computing price per ounce that you used for computing FPI and MCI.

```FanCost\$SodaPPO = FanCost\$Soda/FanCost\$Soda.Oz
FanCost[FanCost\$SodaPPO==max(FanCost\$SodaPPO),]
FanCost[FanCost\$SodaPPO==min(FanCost\$SodaPPO),]
```

## Exercise a, revisited

If you could not figure out the formula for FCI, here it is.

```FanCost\$FCI = 2*FanCost\$Adult + 2*FanCost\$Child + FanCost\$Park +
2*FanCost\$Program + 2*FanCost\$Cap +
2*FanCost\$Beer + 4*FanCost\$Soda + 2*FanCost\$HotDog
```

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.