################# ### Objective ### ################# ## Define the following terms as they relate to R: object, assign, call, function, arguments, options. ## Create objects and assign values to them in R. ## Learn how to name objects. ## Save a script file for later use. ## Use comments to inform script. ## Solve simple arithmetic operations in R. ## Call functions and use arguments to change their default options. ## Inspect the content of vectors and manipulate their content. ## Subset and extract values from vectors. ## Analyze vectors with missing data. ########## # Objects ######### 3 + 5 12 / 7 area_hectares <- 1.0 area_hectares 2.47 * area_hectares area_hectares <- 2.5 2.47 * area_hectares area_acres <- 2.47 * area_hectares area_hectares <- 50 area_acres ################### ### Challenge 1 ### ################### ## What are the values after each statement in the following? length <- 2.5 width <- 3.2 area <- length * width area # change the values of length and width length <- 7.0 width <- 6.5 # does the value of area change? area # how about now? area <- length * width area ############################### # Functions and their arguments ############################### # 1.1 Round pi (3.14159) using the round function # 1.2 Get information about the round function and it's arguments # 1.3 Round pi to 2 digits using the digits argument ######################## # Vectors and data types ######################## # 2.1 Create a numeric vector with the c() function # 2.2 Create a character vector with the c() function # 2.3 Inspect the contents of your vectors using the length(), class(), and str() functions # 2.4 Add more elements to your vector using the c() function # 2.5 Create an integer vector with the colon ################### ### Challenge 2 ### ################### ## Atomic vectors can be of type character, numeric (or double), integer, and logical. ## 1. What happens if we try to mix these types in a single vector? ## HINT: Use class() to check the data type of your objects. ## Why do you think this happens? vec_num_char <- c(1, 2, 3, "a") vec_num_logical <- c(1, 2, 3, TRUE) vec_char_logical <- c("a", "b", "c", TRUE) vec_tricky <- c(1, 2, 3, "4") ## 2. How many values in vec_combined_logical are "TRUE" (as a character) in the following example: vec_num_logical <- c(1, 2, 3, TRUE) vec_char_logical <- c("a", "b", "c", TRUE) vec_combined_logical <- c(vec_num_logical, vec_char_logical) #################### # Subsetting vectors #################### # 3.1 Copy the character vector from 2.2 above or create one # 3.2 Extract/Display a single value from the vector # 3.3 Extract/Display multiple values from the vector # 3.4 Create a logical vector using a logical test on a vector # 3.5 Use the logical test to select values from the vector # 3.6 Use the %in% operator to check each element of a vector against another possessions <- c("car", "bicycle", "radio", "television", "mobile_phone") possessions %in% c("car", "boat") #################### # Missing data #################### # 4.1 Create a vector that includes the NA value # 4.2 Find the mean and max values of the vector # 4.3 Use is.na() to extract elements that are NOT missing values # 4.4 Use na.omit() to return the object with incomplete cases removed # 4.5 Use complete.cases() to extract those elements which are complete cases ################### ### Challenge 4 ### ################### rooms <- c(1, 2, 1, 1, NA, 3, 1, 3, 2, 1, 1, 8, 3, 1, NA, 1) ## 1. Using this vector of rooms, create a new vector with the NAs removed. ## 2. Use the function median() to calculate the median of the vector. ## 3. Use R to figure out how many households in the set use more than 2 rooms for sleeping. #################### # Prepare for Data #################### ## 5.1 Create a subdirectory for the raw data dir.create("data") ## 5.2 Put the data file in the directory ## The data is at https://ndownloader.figshare.com/files/11492171 download.file("https://ndownloader.figshare.com/files/11492171", "data/SAFI_clean.csv", mode = "wb") ## 5.3 Install the Tidyverse Package ## This only needs to be done once on a computer. install.packages("tidyverse") ## 5.4 Test that the tidyverse package loads properly ## This needs to be doen every time R is started library(tidyverse)