Introduction
######################## Preamble ###############################
#This is the most important code -- This CLEARS the working environment
rm(list=ls(all=TRUE))
#************************* Options ************************************
options(stringsAsFactors = FALSE)
options(scipen = 3)
#################### Set the Working Directory #####################
#*************** Instructions to Set the Working Directory ******************
#Set Working Directory - Use the Drop Downs to navigate to the folder!!!
#First go to "Session" -> "Set Working Directory" -> "Choose Directory"
#Then Navigate to the folder your data is located
#If on a PC: Click "Select Folder" / If on a Mac: Click "Open"
#************* !!!IMPORTANT: PUT YOUR WORKING DIRECTORY BELOW: !!! ***************************
Recode with ifelse
# We are going to use the following Variables:
# gear
# Check the unique values in each variable
unique(mtcars$gear)
## [1] 4 3 5
unique(mtcars$cyl)
## [1] 6 4 8
# Recode using ifelse
# First, we will create a new variable called "gear2" and recode 3 in gear into "three", else mtcars$gear
# argument:
# df$new_var <- ifelse(df$old_var %in% "old_value", "new_value", no)
# in the code we are telling R to create a new variable called gear2, and to code it as as "three" if gear=3, else give it values within gear.
mtcars$gear2 <- ifelse(mtcars$gear %in% "3", "three", mtcars$gear)
# Lets check to see if worked
# We should see three elements "three", 4, and 5
table(mtcars$gear2) # it works!
##
## 4 5 three
## 12 5 15
Create a new variable and recode with mutate and ifelse
# We are going to create a new variable "gears3" with the values "three", "four", and "five".
# There is no limit to the number of ifelse statements that we can nest into the mutate argument.
# This is particularly useful if we want to change many elements within a column.
# argument:
# mutate(df, new_var =
# ifelse (old_var == old_value, new_value,
# ifelse (old_var == old_value, new_value,
# ifelse (old_var == old_value, new_value, NA))))
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
mtcars <- mutate(mtcars, gear3 =
ifelse(gear== 3, "three",
ifelse(gear== 4, "four",
ifelse(gear== 5, "five", NA))))
# Check to see if it worked
table(mtcars$gear3, mtcars$gear) # it works!
##
## 3 4 5
## five 0 0 5
## four 0 12 0
## three 15 0 0
Using ifelse to recode values into NA
# Lets create a dataset with NAs
library(randomNames)
## Warning: package 'randomNames' was built under R version 4.0.5
## Error in get(genname, envir = envir) : object 'testthat_print' not found
set.seed(1992)
age <- sample(18:30, 50, replace=TRUE)
score <- sample(1:100, 50, replace=TRUE)
year <- sample(c("first", "second", "third", "fourth"), 50, replace=TRUE)
major <- sample(c("ps", "cs", "ir", "stats"), 50, replace =TRUE)
attend <- sample(c("yes", "no"), 50, replace=TRUE)
office <- sample(c("always", "sometimes", "never", "not applicable", "don't know"), 50, replace=TRUE)
gender <- sample(c("male", "female"), 50, replace=TRUE)
student <- randomNames(50)
class <- data.frame(age, score, year, major, attend, office, gender, student)
# The variable office has two values that we want to recode to NA's (not applicable, don't know)
table(class$office)
##
## always don't know never not applicable sometimes
## 10 8 11 13 8
class$office <- ifelse(class$office %in% c("not applicable", "don't know"), NA, class$office)
table(class$office) # it worked!
##
## always never sometimes
## 10 11 8
unique(class$office) # it worked!
## [1] NA "always" "never" "sometimes"