#sapply
📝 "Apply Family Notebook"

Master R's "Apply" family functions with this quick guide.

👤 Meghan Harris

🔗 https://www.thetidytrekker.com/post/applyfamilynotes/apply-family.html

#rladies#rstats#oldiebutgoodie#r#rprogramming#baser#apply#lapply#sapply#functionalprogramming
November 8, 2026 at 1:56 PM
No curso de Programação em R, traduzi as lições Olhando os Dados, lapply e sapply, vapply e tapply. Também corrigi o caminho dos dados que impedia três lições de abrir depois de instaladas.
September 17, 2026 at 3:27 PM
TIL what the heck grepl does in R. I suspect this is another embarrassing thing I have been coding “around” for years.

My previous horrors were grep(TRUE, …) because I didn’t know about which and lapply(as.list(), …) because I didn’t know about sapply.

Am I the only one who does this?
June 11, 2025 at 3:14 PM
Sad, sorry, sapply
Live, laugh, lapply
March 7, 2026 at 4:44 PM
RT @CMastication [blog & video] example of how to use plyr to address sapply() frustrations in #rstats http://ow.ly/L3QX
Cerebral Mastication - Cerebral Mastication - Something t...
ow.ly
December 1, 2024 at 10:56 PM
do you want to know what i think about how the world should be run?

...no?

too bad. here's my sapply values
August 12, 2026 at 1:30 PM
I only write the most elegant R.
August 14, 2023 at 4:02 PM
Excuse my language / but sometimes only one ugly word works / Fuck you Trump. Irony is that the times I have been blessed to make love to a woman, which brings us back to the F word, ste the best memories of my life as I roll towards 79. Seems so contradictory for same word sapply in both cases
December 20, 2024 at 12:30 AM
Cf sec. A in the supplement to the BMJ article, where you multiply death pdf for one population with survival for other.

R for Gompertz (u periods older):
gouts <- function(r0, a, u, x) gmort(r0,a,x)*gsurv(r0,a,x) * gsurv(gmort(r0,a,u), a, x)

sum(sapply(0:1000, gouts, r0=0.0005, a=0.005, u=100))
August 21, 2025 at 8:37 AM
Not fully equivalent but it gets the for-less job done 😄 (Blergh, I felt so dirty writing this code.)
December 17, 2024 at 2:20 PM
Find out what's taking up all your RAM in R:
sort( sapply(ls(),function(x){object.size(get(x))})) via https://stackoverflow.com/questions/1395270/determining-memory-usage-of-objects #rstats
May 23, 2025 at 3:34 PM
you can speed up the R version by a *huge* factor by iterating only on items present in both groups.
December 1, 2024 at 9:58 PM
I can't sleep so I did the sapply values test for funsies.
August 28, 2024 at 4:31 AM
Please read matloff.github.io/TidyverseSkeptic. Or note this quick point: Part of the tidyverse philosophy is not write loops; use functional programming (FP) instead, meaning replace loops with function calls, e.g. sapply in base-R. thread 1/
matloff.github.io
March 11, 2026 at 8:11 PM


slidingWindow <- function(x,windowWidth,f,matTransp=TRUE) {
n <- length(x)
tmp <- sapply(1:(n-windowWidth+1),
function(j) { f(x[j:(j+windowWidth-1)]) })
if (is.matrix(tmp) && matTransp) return(t(tmp))
tmp
}

2/
December 8, 2024 at 5:43 AM
this definitely brings up some bad, uh, let's call them memories, but the sapply political compass using 3 axes of economic/social/cultural as the main splitting points is honestly the least worst way of framing, since otherwise things like "pro lgbq+ anti gun" doesn't square well.
December 1, 2025 at 5:49 PM
quickr recently gained support for OpenMP!
Just add a `declare(parallel())` above a `for` or `sapply()`!

github.com/t-kalinowski...
January 28, 2026 at 11:24 PM
Conquering R’s Apply Family: Your Guide to apply(), lapply(), sapply(), and tapply()

"Introduction
Welcome, fellow R warriors! Today, we delve into the heart of vectorized operations wit..."

https://www.r-bloggers.com/2024/02/conquering-rs-apply-family-your-guide-to-apply-lapply-sapply-and-tapply/
Conquering R’s Apply Family: Your Guide to apply(), lapply(), sapply(), and tapply()
Introduction Welcome, fellow R warriors! Today, we delve into the heart of vectorized operations with R’s “apply” family: apply(), lapply(), sapply(), and tapply(). These functions are your secret wea...
www.r-bloggers.com
February 15, 2024 at 5:00 AM
Darren's R Top Tips!
A simple way to avoid the failing of "table" in the previous tip would be to sum over a testable condition and use sapply over a range, effectively like a column of SUMIFs in Excel.
sapply(1:10, \(i) {sum(c(1,1,2,3,4,5,6,6,6,8,1,2)==i)})
March 10, 2025 at 10:45 AM
Darren's R Top Tips!
Overlooked R function: replicate()
This is quite different to rep(), and is basically a wrapper for sapply, e.g.
replicate(3,list("a",2,3))
Note it produces here a matrix, not just a longer list or vector.
March 5, 2025 at 10:45 AM
Darren's R Top Tips!
You don't need a for loop or sapply to vectorise a testable condition. Instead, just use the ifelse() function. E.g.
ifelse((1:10)%%2, "daddy", "chips")
December 9, 2024 at 10:45 AM
Darren's R Top Tips!
R can vectorise a lot, but where it can't, you don't always need to use a for loop. The "sapply" function is great for applying a function to each member of a vector and putting the results into a vector, e.g.
sapply(1:10, function(i) i*i)
Here, i*i will do of course.
December 4, 2024 at 10:45 AM
Yeah, it is true that the result deviates from the expectations of ordering variables/observations. If the matrix format that sapply outputs isn’t convenient you can use lapply instead, or you can just transpose it
May 22, 2025 at 12:31 AM
I think this is because you are not supposed to use sapply on matrices. If you do it probably gets coerced into a data.frame, whose elements are the columns.

Anyways, not long ago I discovered asplit() which is pretty handy when needing the flexibility to work across either margin
May 21, 2025 at 11:55 PM