# install.packages("tidyverse")
# install.packages("here")
library(tidyverse)
library(here)
## Load the data
<- readRDS(file = here::here("raw_data", "characters.rds"))
characters <- read.csv(
psych_stats file = here::here("raw_data", "psych_stats.csv"),
sep = ";"
)
Missing values: Exercises
Exercise 1
- Does the
characters
data 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
characters
data frame, so copy it into the new objectcharacters_na
before doing anything. Then set thename
toNA
in the rows34, 103, 300
and theuni_name
toNA
in 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
characters_na
c(34, 103, 300), "name"] <- NA
characters_na[c(404, 670), "uni_name"] <- NA characters_na[
- Remove all rows containing missing values in the column
name
from thecharacters_na
data frame.
Solution
<- characters_na[!is.na(characters_na$name), ] characters_na
Or:
library(tidyverse)
<- characters_na %>%
characters_na drop_na(name)