Consider the following R code that creates a data frame and then drops rows with missing values using drop_na(). What will be the resulting data frame?
library(tidyr) df <- data.frame(x = c(1, 2, NA, 4), y = c("a", NA, "c", "d")) df_clean <- drop_na(df) df_clean
Remember, drop_na() removes any row that has NA in any column.
The drop_na() function removes rows with any missing values. Rows 2 and 3 have NA in either x or y, so only rows 1 and 4 remain.
Given the following data frame with missing values, what will be the result after using fill() to fill missing values in column z?
library(tidyr) df <- data.frame(z = c(NA, 2, NA, 4, NA)) df_filled <- fill(df, z) df_filled
fill() fills missing values by carrying the last non-missing value forward by default.
The fill() function fills NA values by propagating the last observed non-NA value downward. The first NA remains because there is no previous value to fill from.
Which of the following statements about drop_na() in R's tidyr package is correct?
Think about what it means to 'drop' rows with missing data.
drop_na() removes any row that contains NA in any column by default. It does not replace or fill values.
Given the data frame below, what will be the output after using fill() with direction = "up" on column a?
library(tidyr) df <- data.frame(a = c(NA, 3, NA, 7, NA)) df_filled <- fill(df, a, .direction = "up") df_filled
fill() with direction = "up" fills missing values by carrying the next non-NA value backward.
Using direction = "up" fills NA values by propagating the next non-NA value upward. The last NA remains because there is no following value to fill from.
Examine the code below. It tries to fill missing values in column val but produces an error. What is the cause?
library(tidyr) df <- data.frame(val = c(1, NA, 3)) fill(df, val, direction = "down")
Check the exact argument names in the fill() function.
The fill() function expects the argument .direction (with a dot) to specify fill direction. Using direction without the dot causes an error.