---
title: "STAT 2450 Assignment 2, Winter 2020 - due January 27, at beginning of class"
author: "Your name here"
date: 'Banner:  B00??????'
output:
  html_document: default
  pdf_document: default
  word_document: default
---

This exercise relates to the College data set, which can be found in
the file at http://faculty.marshall.usc.edu/gareth-james/ISL/College.csv 
It contains a collection of variables for a number of universities and colleges in the US.  The variables are described in question 8, chapter 2, of
*An Introduction to Statistical Learning*.  

1. Use the *read.csv()* function to read the data into R. Call the
data frame *college*.  Use the *dim()* function to find the dimensions
of the data set.

```{r eval=F}
college=read.csv( ...
attach(college)
```
How many records are there in the data set?  

2. Look at the data using the *fix()* or *edit()* function. You should
notice that the first column is just the name of each university. We
don’t really want R to treat this as data. However, it may be handy
have these names for later.  Try the following commands, which will
copy the college names into row labels which are not part of the data
set, but rather as labels, and then delete the associated column.

```{r eval=F}
rownames(college)= specify the first column of college
college= drop the first column
college[1:2,] 
```

You should see that the first data column is Private. Note
that another column of labels now appears before the
Private column. This is not a data column but contains
the labels that R is giving to each row. If you look at the data
with *fix()* or *edit()*, youu will see that these labels are
called "row.names"."

3.  Use the *summary()* function to produce a numerical summary
of the variables in the data set.

```{r eval=F}
call summary() here
```

4. You will note that all variables are quantitative, except for
"Private", which is a factor variable taking the values "Yes" and "No".  It may be  more convenient to work with numeric variables. 
  (a) Use the "ifelse" command
to define a new variable Private2 which takes the value 1 if Private is "Yes",
and otherwise takes the value 0, as follows:

"Private2=ifelse(Private=="Yes", value 1 here, value 2 here)"

```{r eval=F}
Private2= ifelse function here
```
 
  4(b ) Make a two way frequency table of Private vs Private2, as

"table(Private,Private2)".  

```{r eval=F}
call table() here
```
This will tell you whether the "ifelse" command worked.  Do Private
and Private 2 carry the same information?

5. Use the pairs() function to produce a scatterplot matrix of
the columns 3 through 8 of the data. Recall that
you can reference columns 3-8 of  A using
A[,3:8].

```{r eval=F}
execute the pairs command here
```

6.
  (a) Create a new qualitative variable, called Elite, by binning
the Top10perc variable. We are going to divide universities
into two groups based on whether or not the proportion
of students coming from the top 10 % of their high school
classes exceeds 50 %.

```{r eval=F}
Elite=rep(0,nrow(college))
Elite[college$Top10perc >50]=1
```
  (b) Use the *ifelse()* command to calculate a variable Elite2, 
which should be indentical to Elite.
```{r eval=F}
Elite2=ifelse command here
```

   (c) Use a for loop, and the  
*if(condition){statement 1} else {statement 2}* construct, to define 
a variable Elite3, 
which should be indentical to Elite.

```{r eval=F}
N=dim(college)[1]
Elite3=rep(NA,N)
for (i in 1:N)
if(college$Top10perc[i] >50){assign Elite3[i] here} else
   {assign Elite3[i] here}
```

  (d) Use the table command to create two way frequency tables of
  Elite vs Elite2 and Elite vs Elite3. Then print the tables.  

```{r,eval=F}
table(Elite,Elite2)
table(Elite,Elite3)
```

Do the three variables carry the same information?

  (e)
There are subtle difference in the way that character and factor
variables are treated. Use an *ifelse()* statement to define a character
variable Elitec which takes the value "Yes" if the school is an elite
school, and otherwise "No".  Then convert the character variable to a
factor variable, using the command *Elitef=as.factor(Elitec)*.  Use
the command "table(Elitec,Elitef)" to see if the character and
factor variables carry the same information.  Use the *summary()*
function on each of Elitec and Elitef to see if they are treated the
same.  Similarly, use *boxplot(Outstate~Elitef)* and/or
*boxplot(Outstate~Elitec)* to see whether *boxplot()* accommodates
character and/or factor variables.

```{r eval=F}
Elitec=ifelse command here 
Elitef=as.factor(Elitec)
make a two way table here
summary(Elitec) 
summary(Elitef) 
```
Do Elitec and Elitef carry the same information?  Do the *summary()*
and *boxplot()* functions work with the character form?

```{r eval=F}
boxplot(Outstate~Elitef)
```
```{r eval=F}
boxplot(Outstate~Elitec)
```


(f) To further explore the *if(){} else{}*" construct, try the following:

"if(college$Top10perc>50){Elite4=1} else {Elite4=0}"

```{r eval=F}
if(college$Top10perc>50){Elite4=1} else {Elite4=0}
Elite4
```
What went wrong?  Explore the help info on ifelse and if.
Better yet, make up some of your own examples to see how they
work.
How do  the *ifelse()* and *if(){ } else {}*  constructs differ?

7. A student wishes to find colleges for which 

((the student faculty ratio S.F.Ratio is less than 14) OR
(the proportion of teachers with PhD's , PhD, is greater than 85))
AND (the graduation rate, Grad.Rate, is greater than 70%).   

By using a suitable combination of AND (&), OR (|), < and > operators,
create a new data frame college2 which contains all such colleges.
How many  colleges in the data set 
satisfy the specified conditions?

```{r eval=F}
college2= appropriate command subset *college*
```
How many colleges meet the specified conditions?






