Skip to content
Learn Netverks

Lesson

Step 30/36 83% through track

apply-family

Apply family

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
advanced

This lesson

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

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

You will apply Apply family 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 pointers, structs, and basic control flow from intermediate lessons are familiar.

apply(), sapply(), lapply(), and mapply() iterate without explicit for loops—precursors to purrr (local) in tidyverse.

sapply on vectors

nums <- c(1, 2, 3, 4)
print(sapply(nums, function(x) x * 2))

apply on matrices

m <- matrix(1:6, nrow = 2)
print(apply(m, 1, sum))  # row sums

Margin 1 = rows, 2 = columns—read apply docs carefully.

Important interview questions and answers

  1. Q: sapply vs lapply?
    A: lapply always returns a list; sapply tries to simplify to vector/matrix.
  2. Q: When vectorization enough?
    A: Prefer vectorized ops over apply when native—apply shines on complex row/column logic.

Self-check

  1. What margin sums rows in apply?
  2. What does sapply return for a numeric vector input?

Tip: Check whether native vectorization already does the job before reaching for apply.

Interview prep

apply margins?

1 = rows, 2 = columns on matrices.

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

  • sapply pitfalls?
  • vapply when?

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