0
0
R Programmingprogramming~10 mins

lapply and sapply in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - lapply and sapply
Start with a list/vector
Apply function to each element
lapply: returns list
Output: list
Both lapply and sapply apply a function to each element of a list or vector. lapply always returns a list, while sapply tries to simplify the result to a vector or matrix if possible.
Execution Sample
R Programming
x <- list(a=1:3, b=4:6)
l1 <- lapply(x, sum)
s1 <- sapply(x, sum)
print(l1)
print(s1)
This code applies the sum function to each element of list x using lapply and sapply, then prints both results.
Execution Table
StepActionInput ElementFunction AppliedResult for ElementOutput Type
1Apply sum to x$a1, 2, 3sum6list element
2Apply sum to x$b4, 5, 6sum15list element
3lapply outputNANAlist(a=6, b=15)list
4sapply outputNANAc(a=6, b=15)named numeric vector
5EndNANAExecution completeNA
💡 All elements processed; lapply returns list, sapply returns simplified vector
Variable Tracker
VariableStartAfter Step 1After Step 2Final
xlist(a=1:3, b=4:6)list(a=1:3, b=4:6)list(a=1:3, b=4:6)list(a=1:3, b=4:6)
l1NULLlist(a=6)list(a=6, b=15)list(a=6, b=15)
s1NULLnumeric(0)numeric(0)c(a=6, b=15)
Key Moments - 3 Insights
Why does lapply always return a list even if the function returns a single number?
Because lapply is designed to keep the output structure consistent as a list, so each element's result is stored as a list element regardless of its type (see execution_table rows 3).
Why does sapply sometimes return a vector instead of a list?
sapply tries to simplify the output. If all results are single numbers, it returns a numeric vector instead of a list (see execution_table row 4).
What happens if the function returns different types or lengths for elements when using sapply?
sapply will return a list if it cannot simplify the results to a vector or matrix, preserving the original structure.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output type of lapply after processing all elements?
Anumeric vector
Blist
Cmatrix
Ddata frame
💡 Hint
Check the 'Output Type' column in execution_table row 3.
At which step does sapply produce a simplified numeric vector output?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' and 'Output Type' columns in execution_table row 4.
If the sum function returned vectors instead of single numbers, how would sapply's output change?
AIt would return a list instead of simplifying
BIt would still return a numeric vector
CIt would return NULL
DIt would cause an error
💡 Hint
Refer to key_moments about sapply simplification behavior when results differ in length or type.
Concept Snapshot
lapply(x, FUN): applies FUN to each element of x, always returns a list.
sapply(x, FUN): applies FUN and tries to simplify output to vector or matrix.
Use lapply when you want consistent list output.
Use sapply for simpler output if possible.
Both work on lists or vectors.
Full Transcript
This visual execution shows how lapply and sapply work in R. Both apply a function to each element of a list or vector. lapply always returns a list, keeping each result as a list element. sapply tries to simplify the output: if all results are single values, it returns a numeric vector instead of a list. The example applies sum to each element of a list with two numeric vectors. lapply returns a list with sums, sapply returns a named numeric vector. If the function returns different types or lengths, sapply will return a list to preserve structure. This helps beginners understand when to use each function and what output to expect.