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
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
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?
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?
...no?
too bad. here's my sapply values
...no?
too bad. here's my sapply values
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))
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))
sort( sapply(ls(),function(x){object.size(get(x))})) via https://stackoverflow.com/questions/1395270/determining-memory-usage-of-objects #rstats
sort( sapply(ls(),function(x){object.size(get(x))})) via https://stackoverflow.com/questions/1395270/determining-memory-usage-of-objects #rstats
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/
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/
Just add a `declare(parallel())` above a `for` or `sapply()`!
github.com/t-kalinowski...
Just add a `declare(parallel())` above a `for` or `sapply()`!
github.com/t-kalinowski...
"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/
"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/
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)})
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)})
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.
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.
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")
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")
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.
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.
Anyways, not long ago I discovered asplit() which is pretty handy when needing the flexibility to work across either margin
Anyways, not long ago I discovered asplit() which is pretty handy when needing the flexibility to work across either margin