# install.packages("tidyverse")
# install.packages("here")
library(tidyverse)
library(here)
## Load the data
characters <- readRDS(file = here::here("raw_data", "characters.rds"))
psych_stats <- read.csv(
file = here::here("raw_data", "psych_stats.csv"),
sep = ";"
)Missing values: Exercises
Exercise 1
- Does the
charactersdata set contain anyNAs?
Hint
Use any() to see if a logical vector contains any TRUE values.
Solution
any(is.na(characters))[1] FALSE
No, there don’t seem to be any NAs in this data set, which would be great in real life. For this exercise it’s not great, so let’s introduce some NAs manually.
- Be careful not to overwrite the
charactersdata frame, so copy it into the new objectcharacters_nabefore doing anything. Then set thenametoNAin the rows34, 103, 300and theuni_nametoNAin the rows404, 670.
Hint
To overwrite values, you can select them on the left side of the assignment operator <- and assign them a new value on the right side.
Solution
characters_na <- characters
characters_na[c(34, 103, 300), "name"] <- NA
characters_na[c(404, 670), "uni_name"] <- NA- Remove all rows containing missing values in the column
namefrom thecharacters_nadata frame.
Solution
characters_na <- characters_na[!is.na(characters_na$name), ]Or:
library(tidyverse)
characters_na <- characters_na %>%
drop_na(name)