What will happen here?
Answer at pollev.com/<name>
01:00
What will happen here?
Answer at pollev.com/<name>
01:00
What will happen here?
Answer at pollev.com/<name>
01:00
What will happen here?
Answer at pollev.com/<name>
01:00
Console
Environment
Editor
File Directory
Now we are going to switch over to RStudio to understand these 4 components a bit better.
Console: Where the live R session lives. Type commands into the prompt >
and press enter/return to run them. The Console is in the lower-left pane.
Environment: The space that keeps track of all of the data and objects that you have created or loaded and have access to. Found in the upper right pane.
Editor: Used to compose and edit text (.qmd files) and R code (.r files). Found in the upper left pane.
File Directory: Used to navigate between your files/folders on your Rstudio account. Can move, copy, rename, delete, etc. Found in the lower right pane.
R allows all of the standard arithmetic operations.
R allows all of the standard arithmetic operations.
What is three times one point two raised to the quantity thirteen divided six?
01:00
You can create/save objects using the assignment operator <-
. This is the equivalent of =
in other programming languages. . . .
In order to be recognized as a valid object name, you have to follow certain conventions; namely, the object name should begin with a letter.
good names | names that cause errors |
---|---|
a | 1trial |
b | $ |
FOO | ^mean |
my_var | my var |
A vector is the simplest structure used in R to store data. It can be created using the function c()
.
Create a vector named vec
with the even integers between 1 and 10 as well as the number 99 (six elements total).
Find the sum of that vector.
Find the max of that vector.
Take the mean of that vector and round it to the nearest integer.
These should all be solved with R code. If you don’t know the name of a function to use, with hazard a guess by looking for a help file (e.g. ?sum
) or google it.
05:00
Demo of:
You can combine vectors into a data frame using data.frame()
1
Create an .r script, name it, and save it.
Create three vectors, name
, favorite_color
, and favorite_number
that contain observations on those variables from 5 people in this class.
Combine them into a data frame called my_classmates
.
06:00
R has a vast ecosystem of packages that add new functions. Any installed package can be loaded with the library()
function.
Most data you will not be creating by hand. You will either be
Loading it in from a separate file.
Loading it from within an R package (most of our are in stat20data
)
To load data from a package,
library()
View(<df name>)
.# A tibble: 333 × 8
species island bill_length_mm bill_depth_mm flipper_…¹ body_…² sex year
<fct> <fct> <dbl> <dbl> <int> <int> <fct> <int>
1 Adelie Torgersen 39.1 18.7 181 3750 male 2007
2 Adelie Torgersen 39.5 17.4 186 3800 fema… 2007
3 Adelie Torgersen 40.3 18 195 3250 fema… 2007
4 Adelie Torgersen 36.7 19.3 193 3450 fema… 2007
5 Adelie Torgersen 39.3 20.6 190 3650 male 2007
6 Adelie Torgersen 38.9 17.8 181 3625 fema… 2007
7 Adelie Torgersen 39.2 19.6 195 4675 male 2007
8 Adelie Torgersen 41.1 17.6 182 3200 fema… 2007
9 Adelie Torgersen 38.6 21.2 191 3800 male 2007
10 Adelie Torgersen 34.6 21.1 198 4400 male 2007
# … with 323 more rows, and abbreviated variable names ¹flipper_length_mm,
# ²body_mass_g
tidyverse
The tidyverse
package contains several functions used to manipulate data frames:
select()
: subset columnsarrange()
: sort rowsmutate()
: create a new column from existing column(s)# A tibble: 333 × 2
species island
<fct> <fct>
1 Adelie Torgersen
2 Adelie Torgersen
3 Adelie Torgersen
4 Adelie Torgersen
5 Adelie Torgersen
6 Adelie Torgersen
7 Adelie Torgersen
8 Adelie Torgersen
9 Adelie Torgersen
10 Adelie Torgersen
# … with 323 more rows
# A tibble: 333 × 8
species island bill_length_mm bill_depth_mm flipper_…¹ body_…² sex year
<fct> <fct> <dbl> <dbl> <int> <int> <fct> <int>
1 Adelie Dream 32.1 15.5 188 3050 fema… 2009
2 Adelie Dream 33.1 16.1 178 2900 fema… 2008
3 Adelie Torgersen 33.5 19 190 3600 fema… 2008
4 Adelie Dream 34 17.1 185 3400 fema… 2008
5 Adelie Torgersen 34.4 18.4 184 3325 fema… 2007
6 Adelie Biscoe 34.5 18.1 187 2900 fema… 2008
7 Adelie Torgersen 34.6 21.1 198 4400 male 2007
8 Adelie Torgersen 34.6 17.2 189 3200 fema… 2008
9 Adelie Biscoe 35 17.9 190 3450 fema… 2008
10 Adelie Biscoe 35 17.9 192 3725 fema… 2009
# … with 323 more rows, and abbreviated variable names ¹flipper_length_mm,
# ²body_mass_g
You can sort in descending order by wrapping the variable name in
desc()
.
# A tibble: 333 × 9
species island bill_length_mm bill_d…¹ flipp…² body_…³ sex year bill_…⁴
<fct> <fct> <dbl> <dbl> <int> <int> <fct> <int> <dbl>
1 Adelie Torgersen 39.1 18.7 181 3750 male 2007 57.8
2 Adelie Torgersen 39.5 17.4 186 3800 fema… 2007 56.9
3 Adelie Torgersen 40.3 18 195 3250 fema… 2007 58.3
4 Adelie Torgersen 36.7 19.3 193 3450 fema… 2007 56
5 Adelie Torgersen 39.3 20.6 190 3650 male 2007 59.9
6 Adelie Torgersen 38.9 17.8 181 3625 fema… 2007 56.7
7 Adelie Torgersen 39.2 19.6 195 4675 male 2007 58.8
8 Adelie Torgersen 41.1 17.6 182 3200 fema… 2007 58.7
9 Adelie Torgersen 38.6 21.2 191 3800 male 2007 59.8
10 Adelie Torgersen 34.6 21.1 198 4400 male 2007 55.7
# … with 323 more rows, and abbreviated variable names ¹bill_depth_mm,
# ²flipper_length_mm, ³body_mass_g, ⁴bill_index
Remember that you can nest functions.
# A tibble: 333 × 1
bill_index
<dbl>
1 57.8
2 56.9
3 58.3
4 56
5 59.9
6 56.7
7 58.8
8 58.7
9 59.8
10 55.7
# … with 323 more rows
There is a built-in data set to R called mtcars
that has information on cars that appeared in Motor Trend magazine. It’s already loaded and can be accessed as mtcars
.
Create a slimmer data frame that only contains the columns hp
and wt
and save it to mtcars_slim
.
Create a new column called power_to_weight
that is the ratio of hp
to wt
. Save the three-column data frame back over mtcars_slim
.
Sort the data frame in descending order by the power-to-weight ratio.
Hint: look up help files!
08:00
Break
05:00