Part 8 Handling missing values
drop_na
: drop rows containing missing values.
Create a tibble that contains missing (NA) values:
<- tibble(x = c(1, 2, NA, 5),
dfwithNA y = c("a", NA, "b", "c"))
Remove rows that contain NA values with drop_na
:
drop_na(dfwithNA)
## # A tibble: 2 x 2
## x y
## <dbl> <chr>
## 1 1 a
## 2 5 c
replace_na
: change NA values to a selected value (one per column):
# replace NAs by 0s in column "x", and by "k" in column "y"
replace_na(dfwithNA,
list(x=0, y="k"))
## # A tibble: 4 x 2
## x y
## <dbl> <chr>
## 1 1 a
## 2 2 k
## 3 0 b
## 4 5 c
complete
: turns implicit missing values into explicit missing values:
<- tibble(
df patient = c("Patient1", "Patient1", "Patient2", "Patient3", "Patient3"),
treatment = c("A", "B", "A", "A", "B"),
value1 = 1:5,
value2 = 4:8
)
Here we are missing a row for Patient2 / Treatment B: add it and fill in with NA values:
complete(df,
# columns to expand patient, treatment)
## # A tibble: 6 x 4
## patient treatment value1 value2
## <chr> <chr> <int> <int>
## 1 Patient1 A 1 4
## 2 Patient1 B 2 5
## 3 Patient2 A 3 6
## 4 Patient2 B NA NA
## 5 Patient3 A 4 7
## 6 Patient3 B 5 8
If you want implicit missing values to be filled by something else than NA, use the fill parameter:
# we will fill in missing values with 0s in column "value1", and with NAs in column "value2"
complete(df,
patient, treatment, fill=list(value1=0, value2=NA))
## # A tibble: 6 x 4
## patient treatment value1 value2
## <chr> <chr> <dbl> <int>
## 1 Patient1 A 1 4
## 2 Patient1 B 2 5
## 3 Patient2 A 3 6
## 4 Patient2 B 0 NA
## 5 Patient3 A 4 7
## 6 Patient3 B 5 8
- In practice: what if you have
NA
values, along with empty cells and “customized” missing values?
<- tibble(col1=c(1, 2, NA, 5, "", 4),
dfwithNA2 col2=c("a", NA, "b", "c", "d", "missing"))
Replace empty cells and “customized” missing values with NA
with na_if
:
%>%
dfwithNA2 mutate(col1=na_if(col1, ""), col2=na_if(col2, "missing"))
## # A tibble: 6 x 2
## col1 col2
## <chr> <chr>
## 1 1 a
## 2 2 <NA>
## 3 <NA> b
## 4 5 c
## 5 <NA> d
## 6 4 <NA>
HANDS-ON
Back to the starwars
data set:
- Replace NA in
hair_color
with “unknown.” - Remove rows that still contain NA values.
Answer
# Replace NA in `hair_color` with "unknown".
# Remove rows that still contain NA values.
replace_na(starwars, list(hair_color="unknown")) %>%
drop_na()