Almost every script you write will need to load a library. Do this at the top of the script. In the code chunk below, load the tidyverse
and readxl
libraries. You can check if you have a library under the Packages tab in the lower right RStudio pane. You might need to use install.packages("tidyverse")
to install these libraries on your computer, but never put install.packages()
in a script (just type that command directly into the console window).
library(tidyverse)
library(readxl)
The main data types you will use are numeric (integer or double), character, and boolean.
You assign data to variables with the assignment operator <-
. Assign the following variables:
four
to the integer 4one_plus_3
to the sum of the integer 1 and the integer 3double_four
to the number four as a doublelower
to your first name in all lowercase lettersUPPER
to your first name in all uppercase lettersfour <- 4L
one_plus_3 <- 1L + 3L
double_four <- 4
lower <- "lisa"
UPPER <- "LISA"
The comparison operators, such as ==
, >
, <
, >=
, and <=
let you compare data and/or variables. Remember, =
sets the left side of the equation equal to the right side, while ==
checks if the left and right sides are equivalent.
If you assign a variable to the outcome of a comparison, such as is_equal <- 10 == 5 + 5
, the variable will be TRUE if the comparison is true and FALSE if the comparison is false. Assign the following variables to the outcome of the specified comparison:
comp_1_eq_2
: is the integer 1 equal to the integer 2?comp_a_eq_A
: is the character “a” equal to the character “A”?comp_10_lt_20
: is the number 10 less than the number 20?comp_four_gte_one_plus_3
is the variable four
greater than or equal to the variable one_plus_3
comp_1_eq_2 <- 1L == 2L
comp_a_eq_A <- "a" == "A"
comp_10_lt_20 <- 10 < 20
comp_four_gte_one_plus_3 <- four >= one_plus_3
A vector is a list of 1 or more items of the same data type. Create the following variables:
beatles
: a list of first names John, Paul, George, and Ringoone_to_ten
: a list of the integers 1 through 10evens
: a list of the even integers from 2 to 10a_to_e
: the first 5 items in the built-in vector letters
beatles <- c("John", "Paul", "George", "Ringo")
one_to_ten <- 1:10
evens <- c(2, 4, 6, 8, 10) # seq(2, 10, by = 2)
a_to_e <- letters[1:5]
by_tens
: multiply each item in the vector one_to_ten
by 10squares
: make a 10-item vector with the square of the numbers 1 to 10 (multiply each number by itself)a_to_e_1
: use the paste0
function and the a_to_e
variable you created above to create a vector that looks like "a1", "b2", "c3", "d4", "e5"
.by_tens <- one_to_ten * 10
squares <- 1:10 * 1:10 # (1:10)^2
a_to_e_1 <- paste0(a_to_e, 1:5)
You can load data straight from a website using the URL. Use tidyverse functions for reading CSV and Excel files to create the following variables:
infmort
: load data in the CSV file “https://psyteachr.github.io/msc-data-skills/data/infmort.csv” using the URLmatmort
: download the excel file matmort.xls into a directory called data
in the same directory as this file and load the local file using a function from the package readxl
infmort <- read_csv("https://psyteachr.github.io/msc-data-skills/data/infmort.csv")
## Parsed with column specification:
## cols(
## Country = col_character(),
## Year = col_double(),
## `Infant mortality rate (probability of dying between birth and age 1 per 1000 live births)` = col_character()
## )
matmort <- read_xls("data/matmort.xls")