Skip to content

Instantly share code, notes, and snippets.

@certifiedwaif
Created September 22, 2016 01:37
Show Gist options
  • Save certifiedwaif/a16c4acf4b4a74635f6dcc961fc85429 to your computer and use it in GitHub Desktop.
Save certifiedwaif/a16c4acf4b4a74635f6dcc961fc85429 to your computer and use it in GitHub Desktop.
What's a better way to loop over all combinations of vp, vn and vR2, producing p*n, p and R2 and ending up with an n by 3 data frame or matrix?
library(purrr)
vp <- c(10, 20, 50, 100, 500, 1000)
vn <- c(1.1, 1.25, 1.5, 2, 5, 10)
vR2 <- seq(from=0.00, to=1.00, by = 1e-2)
df <- map(vp, function(p) {
map(vn, function(n) {
map(vR2, function(R2) {c(p*n, p, R2)})
})
}) %>% as.data.frame %>% t
rownames(df) <- NULL
colnames(df) <- c("vn", "vp", "vR2")
df <- as.data.frame(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment