I am trying to clean up my code, and I have a bit where I do the same thing multiple times with different samples. I am taking a random sample, and then plotting the SSE plot. If I copy and paste the code four time it works fine, but I just want to loop through and repeat it four times. Is for
the wrong approach here? How can I accomplish this?
df <- iris
for (i in 1:4) {
print(i)
####################################################
# Random sample
sample_df<- df[sample(nrow(df), 20),]
# Remove ID variable
sample_df$Species <- NULL
# SSE plot
fviz_nbclust(sample_df, kmeans, method = "wss")
####################################################
}