I'm brushing up on my R skills and finally feel like I've mastered the strange sweep function e.g.
df <- data.frame(a = 1:3, b = 2:4)
sweep(df, MARGIN = 2, STATS = c(5, 10), FUN = "*")
## a b
## 1 5 20
## 2 10 30
## 3 15 40
and more usefully here, on a tutorial I'm working on implementing a spatial interaction model in R.
They say that a sign you understand something is that you can say it in many ways, and I think this applies more in programming than almost anywhere else. Yet, despite the problem that sweep
solves seeming apply
-esque, I have NO IDEA whether they are to some degree interchangeable.
So, in order to improve my own understanding of R, is there any way to do the above procedure using apply
?