Remember to create a new Markdown file for the following exercises. You will submit your markdown file as a .pdf or a .rmd file.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
An RMarkdown file combines text (Markdown) and R code in a single document. The main parts are:
Headings are written outside code blocks, using #. In the text, you would type # (SPACE), followed by the Title that you want to insert. Similarly, ## produces a section and ### a subsection.
Code blocks are necessary to integrate R code with typed text for your class assignments. Code blocks are created with triple backticks ```, follow by {r}
x <- 5
y <- 7
x + y
## [1] 12
Create a code chunk and use R as a calculator. You should compute the output for \(8+4\), \(9-2\), \(10*5\), and \(8/5\).
8 + 4
## [1] 12
9 - 2
## [1] 7
10 * 5
## [1] 50
8/5
## [1] 1.6
Learn how to use the R console to define variables, understand why variables are useful, and perform basic calculations. Variables are the names given to memory locations that can store any type of data. In R, assignment can be done in three ways:
-1 Simple Assignment (=) -2 Leftward Assignment (<-) -3 Rightward Assignment (->)
Using the assignment operators listed above, define the values from the following inventory list: 25 oranges, 15 strawberries, 150 cups, and 30 raspberries, 300 napkins.
oranges = 25
strawberries = 15
cups = 150
raspberries = 30
napkins = 300
View the values you saved. Using the concatenate function,
c(var1,var2,var3,etc..), create a new variable of fruit
types.
c(oranges, strawberries, raspberries) -> F
Now, compute the total amount of fruit in the store’s inventory. What about the total amount of other supplies?
sum(F)
## [1] 70
sum(napkins, cups)
## [1] 450
Just like the different ways to measure variables (i.e., nominal, ordinal, discrete, and continuous), R has a way of identifying variables. R recognizes Numeric, Integer, Logical, Character, and Complex, though you will most commonly see Numeric and Character.
Using functions such as as.numeric(objectname),
as.factor(objectname), or
as.logical(objectname) can force R to change how it
recognizes the variables. This may occasionally be necessary if R
recognizes a categorical variable as numeric, or vice versa.
Assign the following values as individual objects. Then use the
function class(objectname) to check which type of value
each is.
x = 12.7
y = 15L
z = "Lab2"
a = 3i
c = c(1,2,3)
Convert z above to a numeric variable. What happens?
as.numeric(z) # NAs introduced by coercion
## Warning: NAs introduced by coercion
## [1] NA
Finally, there are many functions built-in to R that you can use
directly, while some require you to download and apply a package to
access them. For instance, we will use the package
tidyverse throughout this semester, which needs to be
loaded in separately.
Innate functions in R include things like mean(),
sqrt(), log(), median(),
sum(), among many others. Remember that the general syntax
for R functions requires an object or a variable on the inside of the
parantheses.
Run a code chunk using the function library() that
applies the function tidyverse
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.6
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.1 ✔ tibble 3.3.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.2
## ✔ purrr 1.2.1
## ── 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
Play around with some of the innate functions – run at least 2-3 and print the output.
mean(oranges, strawberries, raspberries)
## [1] 25
sqrt(strawberries)
## [1] 3.872983
log(oranges)
## [1] 3.218876
Comments in R