Created
February 16, 2016 16:55
-
-
Save jrnold/eefb2aaab7b2480be42e to your computer and use it in GitHub Desktop.
broom boostrap() function using groups
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bootstrap <- function (df, m, block = FALSE) { | |
n <- nrow(df) | |
attr(df, "indices") <- | |
if (is(df, "grouped_df") && length(attr(df, "indices")) && block) { | |
replicate(m, | |
unlist(lapply(attr(df, "indices"), | |
function(x) sample(length(x), replace = TRUE)), | |
recursive = FALSE, use.names = FALSE), | |
simplify = FALSE) | |
} else { | |
replicate(m, sample(n, replace = TRUE) - | |
1, simplify = FALSE) | |
} | |
attr(df, "drop") <- TRUE | |
attr(df, "group_sizes") <- rep(n, m) | |
attr(df, "biggest_group_size") <- n | |
attr(df, "labels") <- data.frame(replicate = 1:m) | |
attr(df, "vars") <- list(quote(replicate)) | |
class(df) <- c("grouped_df", "tbl_df", "tbl", "data.frame") | |
df | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment