Bird
Raised Fist0
R Programmingprogramming~10 mins

Filtering rows in R Programming - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Filtering rows
Start with data frame
↓
Apply filter condition
↓
Check each row against condition
↓
Keep rows where condition is TRUE
↓
Return filtered data frame
↓
End
Filtering rows means checking each row in a data frame and keeping only those that meet a condition.
Execution Sample
R Programming
df <- data.frame(x = 1:5, y = c(10, 20, 15, 5, 30))
filtered_df <- df[df$x > 2, ]
print(filtered_df)
This code keeps rows where the value in column x is greater than 2.
Execution Table
StepRow Indexx ValueCondition (x > 2)Keep Row?Output Row
111FALSENo
222FALSENo
333TRUEYes3, 15
444TRUEYes4, 5
555TRUEYes5, 30
6EndFiltering complete
💡 All rows checked; only rows with x > 2 are kept.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
df$x1,2,3,4,51,2,3,4,51,2,3,4,51,2,3,4,51,2,3,4,5
Condition (x > 2)NAFALSE, FALSE, TRUE, FALSE, FALSEFALSE, FALSE, TRUE, TRUE, FALSEFALSE, FALSE, TRUE, TRUE, TRUEFALSE, FALSE, TRUE, TRUE, TRUE
filtered_dfNA3, 153, 15; 4, 53, 15; 4, 5; 5, 303, 15; 4, 5; 5, 30
Key Moments - 2 Insights
Why do rows with x = 1 and x = 2 not appear in the filtered data?
Because their condition x > 2 is FALSE (see execution_table rows 1 and 2), so they are not kept.
What does the comma after df[df$x > 2, ] mean in R?
It means we are selecting rows where the condition is TRUE and keeping all columns. Omitting the comma (df[df$x > 2]) does the same and keeps all columns.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the condition result for row index 4?
AFALSE
BNA
CTRUE
DError
💡 Hint
Check the 'Condition (x > 2)' column for row 4 in the execution_table.
At which step does the filtered data frame first include a row?
AStep 3
BStep 2
CStep 1
DStep 5
💡 Hint
Look at the 'Output Row' column in execution_table to see when rows start appearing.
If the condition changed to df$x >= 4, which row index would be kept first?
A3
B4
C2
D5
💡 Hint
Check variable_tracker for values of x and think which satisfy x >= 4.
Concept Snapshot
Filtering rows in R:
Use df[condition, ] to keep rows where condition is TRUE.
Condition checks each row's values.
Rows failing condition are dropped.
Comma after condition means keep all columns.
Result is a smaller data frame.
Full Transcript
Filtering rows means selecting only those rows in a data frame that meet a certain condition. In R, you write df[condition, ] where condition is a logical test on rows. Each row is checked: if the condition is TRUE, the row is kept; if FALSE, it is dropped. For example, df[df$x > 2, ] keeps rows where the x column is greater than 2. The comma after the condition means all columns are kept. The output is a new data frame with only the filtered rows.

Practice

(1/5)
1. What does the subset() function do in R when working with data frames?
easy
A. It sorts the data frame by a column.
B. It changes the column names.
C. It adds new columns to the data frame.
D. It selects rows based on a condition.

Solution

  1. Step 1: Understand the purpose of subset() and compare with other options

    The subset() function is used to pick rows from a data frame that meet a specific condition. Changing column names, adding columns, or sorting are done by other functions, not subset().
  2. Final Answer:

    It selects rows based on a condition. -> Option D
  3. Quick Check:

    subset() filters rows [OK]
Hint: Remember: subset() picks rows that match conditions [OK]
Common Mistakes:
  • Thinking subset() changes columns
  • Confusing subset() with sorting functions
  • Using subset() to add data
2. Which of the following is the correct syntax to filter rows where column age is greater than 30 in a data frame df?
easy
A. subset(df, age > 30)
B. subset(df, age => 30)
C. subset(df, age = 30)
D. subset(df, age < 30)

Solution

  1. Step 1: Check the condition syntax and validate the subset syntax

    The correct comparison operator for 'greater than' is >, so age > 30 is correct. subset(df, age > 30) correctly filters rows where age is greater than 30.
  2. Final Answer:

    subset(df, age > 30) -> Option A
  3. Quick Check:

    Use > for greater than in subset() [OK]
Hint: Use >, <, == for conditions inside subset() [OK]
Common Mistakes:
  • Using => instead of >
  • Using = instead of == or >
  • Filtering with wrong comparison signs
3. Given the data frame df <- data.frame(name=c('Anna','Bob','Cara'), age=c(25, 35, 30)), what is the output of subset(df, age >= 30)?
medium
A. Rows with Bob and Cara
B. All rows
C. Rows with Anna and Bob
D. Rows with Anna and Cara

Solution

  1. Step 1: Identify rows where age >= 30 and check which are selected

    Bob has age 35 and Cara has age 30, both satisfy age >= 30. Anna has age 25, which does not meet the condition, so only Bob and Cara rows remain.
  2. Final Answer:

    Rows with Bob and Cara -> Option A
  3. Quick Check:

    Filter keeps rows with age 30 or more [OK]
Hint: Check each row's value against condition carefully [OK]
Common Mistakes:
  • Including rows with age less than 30
  • Confusing >= with >
  • Assuming all rows are returned
4. What is wrong with this code to filter rows where score is less than 50 in data frame df?
subset(df, score < 50 &)
medium
A. The subset function cannot filter numeric columns.
B. The condition ends with an incomplete operator '&'.
C. The comparison operator '<' is invalid in subset.
D. The data frame name is missing.

Solution

  1. Step 1: Analyze the condition syntax and check other options

    The condition ends with '&' which expects another condition after it, but none is provided. Subset can filter numeric columns, '<' is valid, and data frame name is present as 'df'.
  2. Final Answer:

    The condition ends with an incomplete operator '&'. -> Option B
  3. Quick Check:

    Conditions must be complete without dangling operators [OK]
Hint: Avoid ending conditions with & or | without second part [OK]
Common Mistakes:
  • Leaving dangling & or | operators
  • Thinking subset can't filter numbers
  • Misusing comparison operators
5. You have a data frame df with columns age and status. How do you filter rows where age is over 25 and status is 'active'?
hard
A. subset(df, age > 25, status == 'active')
B. subset(df, age > 25 | status == 'active')
C. subset(df, age > 25 & status == 'active')
D. subset(df, age > 25 && status = 'active')

Solution

  1. Step 1: Use correct logical operator for AND and check comparison operators and syntax

    The operator for AND in R is a single ampersand '&', so use age > 25 & status == 'active'. Use '==' to compare values, not '=', and combine conditions inside one subset call.
  2. Final Answer:

    subset(df, age > 25 & status == 'active') -> Option C
  3. Quick Check:

    Use & and == for combined conditions [OK]
Hint: Use & for AND, | for OR, and == for equality in subset() [OK]
Common Mistakes:
  • Using | instead of & for AND
  • Using = instead of == for comparison
  • Separating conditions with commas
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