Try putting all your code in a function and repeat the process using replicate
or rerun
:
library(dplyr)
run_function <- function() {
sample_size <- 10000
df_lean <- df %>%
rownames_to_column('ID') %>%
dplyr::sample_n(sample_size) %>%
column_to_rownames('ID')
cluster_size = 3
pam_fit <- pam(gower_dist, diss = TRUE, k = cluster_size)
pam_results <- df_lean %>%
dplyr::select(-ID) %>%
mutate(cluster = pam_fit$clustering) %>%
group_by(cluster) %>%
do(the_summary = summary(.))
df_result <- df_lean %>%
rownames_to_column('MIN_ESN') %>%
mutate(cluster = pam_fit$clustering)
return(df_result)
}
N <- 100
result <- purrr::rerun(N, run_function())
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…