# Activity 08/25/2021

## STAT 445 / 645 -- 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
The instructor will give a brief introduction to the class to follow up on the introduction to R videos and to help troubleshoot technology questions.


All students using personal devices 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, removing the line `eval=FALSE` above.  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`.  To simply bring a library into use but not install it, you can instead replace the command `install.packages` with the command `library`.  

For subsequent lines of code, you can remove the `eval=FALSE` 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=TRUE}
ls()
```

Notice that there is only an empty character listed if this is a fresh R environment.  Now try the command
```{r, eval=TRUE}
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}
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
We will discuss the questions from activity 2 as a class.


### Activity 3: Exploring the command line interface continued
You can use `rm` to delete objects you no longer need:

```{r, eval=TRUE}
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, error=TRUE}
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
We will discuss the questions from activity 3 as a class.


