Skip to content

Instantly share code, notes, and snippets.

@jrnold
Created February 16, 2016 16:55
Show Gist options
  • Save jrnold/eefb2aaab7b2480be42e to your computer and use it in GitHub Desktop.
Save jrnold/eefb2aaab7b2480be42e to your computer and use it in GitHub Desktop.
broom boostrap() function using groups
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