---
title: "Intro to Data 309"
author: Dr. Josh Thompson
date: today
format: html
---


After you have downloaded R and RStudio open RStudio and play around with the software.  RStudio has four main panes, the Source Editor, the Workspace Browser, the Plots, and the Console - each with various tabs.  Learn more [here](https://teacherscollege.screenstepslive.com/a/1426910-panes-in-rstudio).  

## Realize that R is a calculator.  

Try some calculations in the console.  While you're in the console, browse the other tabs.   

## c()

Next, the function `c()` stands for *collection* and returns a collection, or list.  Make several collections, varying the kinds of elements it contains.  What do you learn about the behavior? 

```{r}
#| label: play

3*4 - 5

x <- c(1:25)
x^2

last_names <- c("phillips",1:5,"lebowski")

```

## packages

Many data sets and much of the functionality of R exists as packages. The code below downloads the package `tidyverse` from CRAN.  See [Section 1.4.3](https://r4ds.hadley.nz/intro#the-tidyverse) of the text. 

```{r}
#| label: load-tidyverse
#| eval: false

install.packages("tidyverse")

```

## library()

The step above merely downloads the package to your machine.  It does not load it in R.  To use the package you *check it out from the library* as follows.

```{r}
#| label: library-call

library(tidyverse)

```

Visit section [1.4.4](https://r4ds.hadley.nz/intro#the-tidyverse)
and run the second "library" command.  Can you explain what happened?

## datasets

Data is everywhere -  you're encouraged to find data on your own.  But to get started we'll use some of the many datasets that are available as packages.  This one is from a scientific study on penguins. 

```{r}
#| label: download-palmer
#| eval: false

 install.packages("palmerpenguins")

```


Remember, this just downloads the data.  You only have to do this once.  To use the data you need to load the package into R each session.

```{r}
#| label: library-palmer
#| echo: false

 library(palmerpenguins)

```

## help

To examine what is in this package you can use `help` as shown below. 

```{r}
#| label: help-library-palmer
#| echo: true

help(package="palmerpenguins")
```

So now you see `penguins` is a data.frame inside this package. You can explore it in a number of ways. You can type `View(penguins)` to view the data in RStudio.  You just type `penguins`, or you can look a summary of the data with `summary(penguins)`.   To see just the beginning of penguins do

```{r}
#| label: head

head(penguins)

```

We have data about the species `r penguins$species[1]`  and its bill length `r penguins$bill_length_mm[1]` mm, among many other things.   We'll learn how to make all kinds of graphics from datasets like this.  One easy thing we can do is compare male and female bill lengths as below. 

Notice the `<-` symbol is an **assignment** of the right side to the left.  

```{r}
a <- c(1:5)
```

## pipe

The `|>` is the **pipe** which sends the left side to the right side.  

```{r}
c(1:5) |> sum()
```

The usage of the pipe may seem weird at first, but it's ubiquitous so **get used to using |>**.

## ggplot

The `ggplot` function is one of the main plotting tools we'll use.  In the syntax of `ggplot` you notice that its first argument is a data frame, but in the code below it only accepts the `aes()` argument.  This is because what precedes the pipe *always* goes into the first argument of what follows.  We'll learn this in detail later.  

```{r}
#| label: plot penguins 1

penguins_complete <- penguins[complete.cases(penguins),]
  ggplot(penguins_complete,aes(x = body_mass_g,y = bill_length_mm, color = sex)) +
geom_point()
```

**is equivalent to**

```{r}
#| label: plot-male-female-bill-lengths

penguins_complete <- penguins[complete.cases(penguins),]
penguins_complete |> 
  ggplot(aes(x = body_mass_g,y = bill_length_mm, color = sex)) +
geom_point()
```

## Assignment 1 

Load a data set referenced in Chapter 1 and create *some* kind of plot from it.  See <https://jonpage.github.io/r-course/intro.html> for inspiration.  Note the syntax to refer to a specific variable `penguins$bill_length_mm`.  
Export the plot as an image and upload to your Samba Share folder.



.