# Activity 08/26/2020

## STAT 757 -- Section 1001 <br> Instructor: Colin Grudzien<br>

## Instructions:
We will work through the following series of activities as a group and hold small group work and discussions in Zoom Breakout Rooms.  Follow the instructions in each sub-section when the instructor assigns you to a breakout room.

## Activities:

### Activity 1: Introduction by the instructor -- questions about technology (10 minutes)
The instructor will give a brief introduction to the class to follow up on the introduction to R videos.  


All students should install the "faraway" and other packages as
```{r, eval=FALSE }
install.packages("faraway")
install.packages("ggplot2")
install.packages("plyr")
install.packages("gapminder")
```
This can be performed by compiling this RMarkdown activity sheet.  If you wish to stop installing the package when you compile the RMarkdown document, you can remove the set the option in the bracket to ```eval=FALSE```.  For subsequent lines of code, you can remove this line to run the code chunk.  The remaining code chunks will not run automatically and you will have to enable this as described above in the below activities in order to proceed through today's lesson.


#### Essential help resources
If you're having trouble using a function, 9 times out of 10,
the answers you are seeking have already been answered on
[Stack Overflow](http://stackoverflow.com/). You can search using
the `[r]` tag.

If you can't find the answer, there are a few useful functions to
help you ask a question from your peers:

```{r, eval=FALSE}
?dput
```

Will dump the data you're working with into a format so that it can
be copy and pasted by anyone else into their R session.

```{r, eval=FALSE}
sessionInfo()
```

Will print out your current version of R, as well as any packages you
have loaded. This can be useful for others to help reproduce and debug
your issue.


#### Other ports of call

You can also check out the following resources for general documentation on the R language.

  * [Quick R](http://www.statmethods.net/)
  * [RStudio cheat sheets](http://www.rstudio.com/resources/cheatsheets/)
  * [Cookbook for R](http://www.cookbook-r.com/)






### Activity 2: Exploring the command line interface in R (5 minutes)

There are a few useful commands you can use to interact with the R session.

`ls` will list all of the variables and functions stored in the global environment
(your working R session):

```{r, eval=FALSE}
ls()
```

Notice that there is only an empty character listed if this is a fresh R environment.  Now try the command
```{r, eval=FALSE}
x <- 1:5
ls()
```

Now we see the variable that we assigned named `x`.

Note here that we didn't give any arguments to `ls`, but we still
needed to give the parentheses to tell R to call the function.

If we type `ls` by itself, R prints a bunch of code instead of a listing of objects.

```{r, eval=FALSE}
ls
```

What's going on here? 

Like everything in R, `ls` is the name of an **object**. 

Entering the name of
an object by itself prints the contents of the object. The object `x` that we
created earlier contains `x`:

```{r, eval=FALSE}
x
```

The object `ls` contains the R code that makes the `ls` function work! 

#### Discuss the following with your peers:

Do you know what an object is in computing?  What are some other examples of objects in R?


### Debrief: discuss the questions (5 minutes)
We will discuss the questions from activity 2 as a class.


### Activity 3: Exploring the command line interface continued (5 minutes)

You can use `rm` to delete objects you no longer need:

```{r, eval=FALSE}
rm(x)
```

If you have lots of things in your environment and want to delete all of them,
you can pass the results of `ls` to the `rm` function:

```{r, eval=FALSE}
rm(list = ls())
```

In this case we've combined the two. Like the order of operations, anything
inside the innermost parentheses is evaluated first, and so on.

In this case we've specified that the results of `ls` should be used for the
`list` argument in `rm`. When assigning values to arguments by name, you *must*
use the `=` operator!!

If instead we use `<-`, there will be unintended side effects, or you may get an error message:

```{r, eval=FALSE}
rm(list <- ls())
```

#### Question 1

What will be the value of each variable  after each
statement in the following program?

```{r, eval=FALSE}
mass <- 47.5
age <- 122
mass <- mass * 2.3
age <- age - 20
```
Can you explain why this is in terms of variable assignment rules?

#### Question 2

Run the code from the previous question, and write a command to
compare mass to age. Is mass larger than age?


#### Question 3

Clean up your working environment by deleting the mass and age
variables.  Can you remove both variables with a single command?


### Debrief: discuss the questions (5 minutes)
We will discuss the questions from activity 3 as a class.



### Activity 4: a review of data types (10 minutes)

With your group, discuss each of the following questions:

#### Question 1: 
What do you expect the result of the following to be?

```{r, eval=FALSE}
1 == TRUE
```
Can you explain in terms of types and type coercion why we get this answer?


#### Question 2: 
Look at the help for the `c` function. What kind of vector do you expect you will create if you evaluate each of the following lines of code?
```{r, eval=FALSE}
c(1, 2, 3)
c('d', 'e', 'f')
c(1, 2, 'f')
```


#### Question 3:

Before we start the exercise, we should have some additional background. There are 5 main types in R: double;

```{r} 
typeof(pi)
```

integer;
```{r}
typeof(1L)
```

complex;
```{r}
typeof(1+1i)
```

and non-numeric types such as logical 
```{r}
typeof(TRUE)
```

and character
```{r}
typeof('banana')
```

Understanding data types and the way they are coerced is important because R strictly enforces that data in a vector is of a single type. By keeping everything in a column the same, we can make simple assumptions about our data;if you can interpret one entry in the column as a number, then you can interpret all of them as numbers, so we don’t have to check every time.   This consistency is one aspect of what people mean when they talk about clean data; in the long run, strict consistency goes a long way to making our lives easier in R.

<ul>
  <li> The coercion rules go: </li>
  <ol>
    <li> logical -> integer</li> 
    <li> integer -> numeric </li>
    <li> numeric -> complex </li>
    <li> complex -> character,</li>
  </ol>
  <li> where -> can be read as are transformed into.</li> 
  <li>You can try to force coercion against this flow using the as. functions, e.g.,</li>
</ul>

```{r}
as.numeric(c('1', '2', '3'))
```  

Consider now how we have seen that vector may coerce a logical value to numeric one:

```{r, eval=FALSE}
c(TRUE, pi)
```

What do you expect the output of the following expression to be?

```{r, eval=FALSE}
c(TRUE, 'TRUE')
```

Can you explain this in terms of the standard coercion rules?  How can you coerce 'TRUE' back into a logical data type?


#### Question 4:


Is the following expression guaranteed to be "TRUE"?

```{r, eval=FALSE}
0 == sin(pi)
```
Discuss with your peers why this is the case.  What is a better option for comparing double precision approximations in R?


#### Question 5:
Consider the following expressions --- what is the final output?  What does this have to do with the slice operator ```:```?

```{r, eval=FALSE}
x <- 9:25
y <- 3:4
x[y]
```

#### Question 6:

Consider the following expressions --- what is the final output?  Can you think of some instances where a logical index vector would be a valuable way to extract data?  Give an example of another comparison.

```{r, eval=FALSE}
x <- 9:25
y <- x > 16
x[y]
```


#### Question 7:

We have seen that we can apply mathematical operations to numerical vectors. 

What do you expect the output to be of the following statement?  Can you explain the output?

```{r, eval=FALSE}
c('red', 'blue', 'green') + 2
```

### Debrief: discuss the questions (5 minutes)
We will discuss the questions from activity 4 as a class.


### Activity 5: A first look at dataframes (10 minutes)
Vectors are one type of data structure that we encounter frequently; however, there are other related (and important) data structures that we will need to manipulate.  R has an extremely developed tool for handling tabular data, such as comma separated values -- this tool is a "dataframe".  We will explore the functionality of dataframes with some "cat" data in some simple examples:
```{r eval=FALSE}
cats <- data.frame(coat = c("calico", "black", "tabby"), 
                    weight = c(2.1, 5.0, 3.2), 
                    likes_string = c(1, 0, 1))
```
Notice that the arguments of the function "data.frame()" are three expressions associating a name with a vector.


Printing the variable "cats", we see what tabular data looks like in a dataframe:
```{r eval=FALSE}
cats
```

The assignment of the vectors to names in the arguments assigned the column names. Each column consists of **a vector of uniform data type**.  If we want to extract a named column from a dataframe, this can be done with the "$" sign and the column name:
```{r eval=FALSE}
cats$weight
```
Each row, on the other hand, consists of multiple measurements (of different data types) corresponding to one specific case of the data set. We might suppose that the scale used to measure the cats' weights was uniformly off by two kgs in all measurements.  

#### Question 1:
How can we can re-assign values into the column of weights?  Think about how to make a recursive assignemnt.
```{r eval=FALSE}
cats$weight <- cats$weight +  2
```

* We can verify that the assignment went into the column for weight in "cats",

```{r eval=FALSE}
cats
```


#### Question 2:
A data structure related to vectors are **lists**.  Lists function as containers for heterogeneous data, allowing different types.  Consider the following lines of code, discuss with your peers the differences in the output from the ```list()``` function and the constructor function ```c()```.  What do you think are some of the practical differences between lists and vectors?  Can you identify the types of elements of the list?

```{r eval=TRUE}
list_example <- list(1, "a", TRUE, 1+4i)
list_example
vector_example <- c(1, "a", TRUE, 1+4i)
vector_example
```

#### Question 3:

In the last example no type coercion has taken place; 
```{r eval=FALSE}
typeof(list_example[[1]])
typeof(list_example[[2]])
```
all of the original types have been respected, but because the data is allowed to be inhomogeneous we can't use vector operations on a list.
```{r eval=FALSE, error=TRUE}
list_example + 2
```
Here we see an error message because the "+" operator only knows how to operate on numeric arguments, or ones that can be coerced into one.


Recall our dataframe cats,
```{r eval=FALSE}
cats
```
We know that dataframes contain homogeneous data in each <b>column</b>, but each row may be inhomogeneous.
**Q:** What kind of data structure do you think a dataframe is? Can it be a vector? Why or why not?  How can you test what type of data strcuture our ```cats``` dataframe is?


### Debrief: discuss the questions (5 minutes)
We will discuss the questions from activity 5 as a class.


