# this is a code chunk
AE 01: Meet the penguins
The goal of this application exercise is to get exposure to using the computational toolkit. Let’s get started!
Quarto Code Chunks
- Code goes in “code chunks”: these are grey boxes and can be recognized by ‘{r}’
- To run a code chunk, click the little green right facing arrow; to run a code chunk and all preceding code chunks, use the downward pointing arrow.
- Text goes outside of the code chunks!
What’s that text in the code chunk?
#| label: code-chunk : this blue text at the top is a label: basically, it names the code chunk for easy reference. Code chunk names cannot be repeated!
\# this is a code chunk : this green text is a comment. A comment goes in a code chunk, but functions like normal text
What’s the difference between a label and a comment?
Load Packages
For this application exercise, we’ll use the tidyverse and palmerpenguins packages.
Warning: package 'tidyr' was built under R version 4.4.1
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.5.1 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.1
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Warning: package 'palmerpenguins' was built under R version 4.4.1
Examine Data
The dataset we will use is called penguins
; it was loaded with the palmerpenguins package. You’ll notice it’s not visible yet in the environment pane - let’s put it there.
data("penguins")
Two useful functions to examine it are glimpse()
and View()
Let’s glimpse()
at it.
-
Your turn: Replace
#add code here
with the code for “glimpse” ing at the datapenguins
data frame –glimpse(penguins)
. Render the document and view the output.
# add code here
Now, let’s View()
it.
-
Your turn: Replace
#add code here
with the code for “view” ing at the datapenguins
data frame –View(penguins)
.
# add code here
What information can you see from these two operations? How are they different?
Some R Fundamentals
You just used some functions above - library()
, data()
, glimpse()
, and view()
. Let’s practice with some more!
Getting Help
There is a function that tells you how many rows are in the data frame: nrow()
. Perhaps this is your first time using it and you aren’t sure how it works: you can use ?
to see the documentation.
-
Your turn: Write code to get help with the
nrow
function
#add code here
(This works for any function, not just nrow
!)
- Your Turn: Now, let’s compute the number of rows in the data frame:
#add code here
Oh no!! Errors!!
Unfortunately, not every time you run a function it will work correctly.
What happens if you run mean()
on the data frame? Does this even make sense???
-
Your turn: try running this function on the
penguins
data frame and see what happens!
# add code here
Accessing Columns
As we saw with the mean
example, not every function works on a full data frame. Sometimes, you need to access just one column. To do that, we can use $
as dataframe$column_name
.
-
Your turn: In the code chunk below, compute the mean of the
bill_depth_mm
variable in thepenguins
data frame.
#add code here
Hmmm… something weird is still happening! What does this NA value mean?? Do you have any guesses??? How can we fix this?
>1 Argument
To fix our issue with mean, we need to tell the function something else (that is, use more than one argument).
-
Your turn: First, get help with the
?
. Then, try to compute themean
value, ignoring the NA values
#add code here
How is the document looking?
Click render to see!
Let’s push our changes to GitHub!
Remember:
Stage changes with the checkboxes
Commit with a message
Push!
Miscellaneous:
If there is extra time in class, we’ll add some other tips here!