|
You are here |
epiverse-trace.github.io | ||
| | | | |
privefl.github.io
|
|
| | | | | In this post, I talk about loops in R, why they can be slow and when it is okay to use them. Don't grow objects Let us generate a matrix of uniform values (max changing for every column). gen_grow <- function(n = 1e3, max = 1:500) { mat <- NULL for (m in max) { mat <- cbind(mat, runif(n, max = m)) } mat } set.seed(1) system.time(mat1 <- gen_grow(max = 1:500)) ## user system elapsed ## 0.333 0.189 0.523 system.time(mat2 <- gen_grow(max = 1:2000)) ## user system elapsed ## 6.183 7.603 13.803 gen_sapply <- function(n = 1e3, max = 1:500) { sapply(max, function(m) runif(n, max = m)) } set.seed(1) system.time(mat3 <- gen_sapply(max = 1:500)) ## user system elapsed ## 0.026 0.005 0.030 identical(mat3, mat1) ## [1] TRUE system.time(mat4 <- gen_sapply(max = 1:2000)) ... | |
| | | | |
aosmith.rbind.io
|
|
| | | | | In this post I delve into the details of the R functions I've been using in my simulation examples, focusing on the replicate() function and the map family of functions from the purrr package. I spend a little time showing the parallels between the replicate() function and a for() loop. | |
| | | | |
zevross.com
|
|
| | | | | The purrr package is a functional programming superstar which provides useful tools for iterating through lists and vectors, generalizing code and removing programming redundancies. The purrr tools... | |
| | | | |
tibble.tidyverse.org
|
|
| | | as_tibble() turns an existing object, such as a data frame or matrix, into a so-called tibble, a data frame with class tbl_df. This is in contrast with tibble(), which builds a tibble from individual columns. as_tibble() is to tibble() as base::as.data.frame() is to base::data.frame(). as_tibble() is an S3 generic, with methods for: data.frame: Thin wrapper around the list method that implements tibble's treatment of rownames. matrix, poly, ts, table Default: Other inputs are first coerced with base::as.... | ||