Skip to content
Learn Netverks

Lesson

Step 16/36 44% through track

subsetting-r

Subsetting

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Subsetting: the syntax, patterns, and safety habits you need before advancing in R.

Teams still ship Subsetting in R codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Subsetting in contexts like: Research pipelines, Shiny dashboards, and statistical reporting.

Write R in the editor and click Run on server—the dev runner executes with Rscript; use print() or cat() and base R in playground snippets (tidyverse locally; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Subsetting extracts parts of vectors, matrices, and data frames with [ ], logical masks, and which(). Master this before dplyr shortcuts.

Logical subsetting

df <- data.frame(name = c("Ada", "Lin"), score = c(92, 88))
high <- df[df$score >= 90, ]
print(high)

which() and names

idx <- which(df$score >= 90)
print(df[idx, , drop = FALSE])

drop = FALSE keeps data frame shape when one row remains.

Important interview questions and answers

  1. Q: [ ] vs [[ ]]
    A: [ ] can return subset structure; [[ ]] extracts a single element/column simplistically.
  2. Q: Logical length match?
    A: Recycling applies—ensure mask length matches nrow for data frames.

Self-check

  1. What does df[df$score >= 90, ] do?
  2. Why use drop = FALSE?

Pitfall: Dropping to a vector loses column names—use drop = FALSE on single-row subsets.

Interview prep

drop = FALSE?

Prevents a one-row subset from becoming a vector and losing column names.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • [ vs [[?
  • drop=FALSE?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump