Bird
Raised Fist0
R Programmingprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the drop_na() function do in R's tidyr package?
easy
A. Fills missing values with the mean of the column.
B. Replaces missing values with zeros.
C. Removes rows that contain missing values in specified columns.
D. Sorts the data frame by missing values.

Solution

  1. Step 1: Understand the purpose of drop_na()

    The function is designed to remove rows that have missing (NA) values in specified columns or in the entire data frame if no columns are specified.
  2. Step 2: Compare with other options

    Options A and C describe filling missing values, which is done by fill() or other functions, not drop_na(). Sorts the data frame by missing values. sorts the data frame by missing values, which is unrelated.
  3. Final Answer:

    Removes rows that contain missing values in specified columns. -> Option C
  4. Quick Check:

    drop_na() removes rows with NA [OK]
Hint: Remember: drop_na drops rows with NA, fill replaces NA [OK]
Common Mistakes:
  • Confusing drop_na() with fill()
  • Thinking drop_na() fills missing values
  • Assuming drop_na() sorts data
2. Which of the following is the correct syntax to fill missing values in a data frame df using the fill() function from tidyr for the column score?
easy
A. df.fill(score, direction = "down")
B. fill(df, score, direction = "down")
C. fill(score, df, .direction = "down")
D. df %>% fill(score, .direction = "down")

Solution

  1. Step 1: Recall the pipe operator usage

    The fill() function is typically used with the pipe operator %>% to pass the data frame as the first argument.
  2. Step 2: Check the argument order and syntax

    df %>% fill(score, .direction = "down") correctly uses df %>% fill(score, .direction = "down"). Options B and C have wrong syntax or argument order. df.fill(score, direction = "down") uses incorrect syntax not valid in R.
  3. Final Answer:

    df %>% fill(score, .direction = "down") -> Option D
  4. Quick Check:

    Use pipe %>% before fill() with column name [OK]
Hint: Use pipe %>% before fill() and specify column [OK]
Common Mistakes:
  • Passing data frame as second argument
  • Using dot direction without dot prefix
  • Using Python-like method syntax
3. Given the data frame df below:
df <- data.frame(id = 1:5, score = c(10, NA, 15, NA, 20))
df_filled <- df %>% fill(score, .direction = "down")
print(df_filled)

What will be the output of print(df_filled)?
medium
A. id score 1 1 10 2 2 10 3 3 15 4 4 15 5 5 20
B. id score 1 1 10 2 2 NA 3 3 15 4 4 NA 5 5 20
C. id score 1 1 10 2 2 15 3 3 15 4 4 20 5 5 20
D. id score 1 1 NA 2 2 NA 3 3 15 4 4 15 5 5 20

Solution

  1. Step 1: Understand fill() with .direction = "down"

    The function fills missing values by copying the last known non-NA value downward to replace NA values.
  2. Step 2: Apply fill() to the data

    Row 2's NA is replaced by 10 (from row 1), row 4's NA is replaced by 15 (from row 3). Other values remain unchanged.
  3. Final Answer:

    id score 1 1 10 2 2 10 3 3 15 4 4 15 5 5 20 -> Option A
  4. Quick Check:

    fill() copies last known value down [OK]
Hint: fill() with down copies last known value to NA below [OK]
Common Mistakes:
  • Assuming fill() replaces NA with next value
  • Thinking fill() leaves NA unchanged
  • Confusing fill() direction
4. Consider the following code snippet:
library(tidyr)
df <- data.frame(x = c(1, NA, 3), y = c(NA, 2, 3))
df_clean <- drop_na(df, x)
print(df_clean)

What is the output of print(df_clean)?
medium
A. x y 1 1 NA 3 3 3
B. x y 2 NA 2 3 3 3
C. x y 1 1 NA 2 NA 2 3 3 3
D. x y 3 3 3

Solution

  1. Step 1: Understand drop_na(df, x)

    This removes rows where column x has NA values. Row 2 has NA in x, so it is removed.
  2. Step 2: Identify remaining rows

    Rows 1 and 3 remain. Row 1 has x=1, y=NA. Row 3 has x=3, y=3.
  3. Final Answer:

    x y 1 1 NA 3 3 3 -> Option A
  4. Quick Check:

    drop_na(df, x) removes rows with NA in x [OK]
Hint: drop_na(df, col) removes rows with NA in that column [OK]
Common Mistakes:
  • Removing rows with NA in all columns instead of specified
  • Confusing drop_na() with fill()
  • Expecting NA values to be replaced
5. You have a data frame df with columns time and temperature, where some temperature values are missing (NA). You want to fill missing temperatures by carrying the last known temperature forward, but only within each day (grouped by time). Which code correctly achieves this?
hard
A. df %>% fill(temperature, .direction = "up") %>% group_by(time)
B. df %>% group_by(time) %>% fill(temperature, .direction = "down")
C. df %>% fill(temperature, .direction = "down") %>% group_by(time)
D. df %>% group_by(time) %>% drop_na(temperature)

Solution

  1. Step 1: Group data by time

    Grouping by time ensures fill() works within each day separately.
  2. Step 2: Use fill() with .direction = "down"

    Filling downward copies last known temperature forward within each group.
  3. Step 3: Check other options

    df %>% fill(temperature, .direction = "down") %>% group_by(time) fills before grouping, so fill applies to whole data ignoring groups. df %>% fill(temperature, .direction = "up") %>% group_by(time) fills upward, which is opposite of requirement. df %>% group_by(time) %>% drop_na(temperature) drops rows with NA instead of filling.
  4. Final Answer:

    df %>% group_by(time) %>% fill(temperature, .direction = "down") -> Option B
  5. Quick Check:

    Group then fill down to fill within groups [OK]
Hint: Group first, then fill down to fill missing within groups [OK]
Common Mistakes:
  • Filling before grouping
  • Using fill direction up instead of down
  • Dropping rows instead of filling
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard