0
0
R Programmingprogramming~20 mins

Handling missing values (drop_na, fill) in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Missing Values Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output after dropping NA values?

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?

R Programming
library(tidyr)
df <- data.frame(x = c(1, 2, NA, 4), y = c("a", NA, "c", "d"))
df_clean <- drop_na(df)
df_clean
A
  x y
2 2 NA
3 NA c
B
  x y
1 1 a
2 2 NA
4 4 d
C
  x y
1 1 a
3 NA c
4 4 d
D
  x y
1 1 a
4 4 d
Attempts:
2 left
💡 Hint

Remember, drop_na() removes any row that has NA in any column.

Predict Output
intermediate
2:00remaining
What is the output after filling missing values?

Given the following data frame with missing values, what will be the result after using fill() to fill missing values in column z?

R Programming
library(tidyr)
df <- data.frame(z = c(NA, 2, NA, 4, NA))
df_filled <- fill(df, z)
df_filled
A
  z
1  2
2  2
3  2
4  4
5  4
B
  z
1 NA
2  2
3  2
4  4
5  4
C
  z
1 NA
2  2
3 NA
4  4
5 NA
D
  z
1 NA
2 NA
3 NA
4  4
5  4
Attempts:
2 left
💡 Hint

fill() fills missing values by carrying the last non-missing value forward by default.

🧠 Conceptual
advanced
1:30remaining
Which statement about drop_na() behavior is true?

Which of the following statements about drop_na() in R's tidyr package is correct?

A<code>drop_na()</code> removes rows with <code>NA</code> in any column by default.
B<code>drop_na()</code> removes rows only if all columns are <code>NA</code>.
C<code>drop_na()</code> replaces <code>NA</code> values with zeros.
D<code>drop_na()</code> fills <code>NA</code> values with the previous non-<code>NA</code> value.
Attempts:
2 left
💡 Hint

Think about what it means to 'drop' rows with missing data.

Predict Output
advanced
2:00remaining
What is the output after filling missing values with direction = 'up'?

Given the data frame below, what will be the output after using fill() with direction = "up" on column a?

R Programming
library(tidyr)
df <- data.frame(a = c(NA, 3, NA, 7, NA))
df_filled <- fill(df, a, .direction = "up")
df_filled
A
   a
1  3
2  3
3  7
4  7
5 NA
B
   a
1  3
2  3
3 NA
4  7
5  7
C
   a
1 NA
2  3
3 NA
4  7
5 NA
D
   a
1 NA
2  3
3  3
4  7
5  7
Attempts:
2 left
💡 Hint

fill() with direction = "up" fills missing values by carrying the next non-NA value backward.

🔧 Debug
expert
2:30remaining
Why does this fill() code produce an error?

Examine the code below. It tries to fill missing values in column val but produces an error. What is the cause?

R Programming
library(tidyr)
df <- data.frame(val = c(1, NA, 3))
fill(df, val, direction = "down")
AThe fill() function requires the column name as a string, not bare name.
BThe data frame has no missing values to fill, so fill() fails.
CThe argument name for direction is incorrect; it should be .direction, not direction.
DThe fill() function cannot be used on numeric columns.
Attempts:
2 left
💡 Hint

Check the exact argument names in the fill() function.