1

i've 6 dataframes from which i take randomly 3 variables each. I want to store the 18 variables in a dataframe and iterate the operation 1000 times creating 1000 dataframes that i want to store in a single list with a for-loop. I've tried like this:

#create 6 clusters

cluster1 = subset(log_ret_stand,select = which(pam.res$clustering == 1))
cluster2 = subset(log_ret_stand,select = which(pam.res$clustering == 2))
cluster3 = subset(log_ret_stand,select = which(pam.res$clustering == 3))
cluster4 = subset(log_ret_stand,select = which(pam.res$clustering == 4))
cluster5 = subset(log_ret_stand,select = which(pam.res$clustering == 5))
cluster6 = subset(log_ret_stand,select = which(pam.res$clustering == 6))

#Create 1000 ptf from clusters

list_ptf = list ()
for (i in 1:1000) {
list_ptf[[i]] = assign(paste0("ptf", i), data.frame(log_ret_stand[,1], cluster1[, sample(ncol(cluster1), 3)], cluster2[, sample(ncol(cluster2), 3)],cluster3[, sample(ncol(cluster3), 3)], cluster4[, sample(ncol(cluster4), 3)], cluster5[, sample(ncol(cluster5), 3)], cluster6[, sample(ncol(cluster6), 3)]))
}

I'm newbe on R and i know that my codes are very basics. Thank you for your attentions

  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. You can probably just do `clusters <- split( log_ret_stand, pam.res$clustering)` – MrFlick Oct 02 '19 at 15:32
  • Possible duplicate of [Split a large dataframe into a list of data frames based on common value in column](https://stackoverflow.com/questions/18527051/split-a-large-dataframe-into-a-list-of-data-frames-based-on-common-value-in-colu) – camille Oct 02 '19 at 17:18

1 Answers1

1

Storing all the data frames in a list is a great idea. The assign function creates global variables which is a bad idea.

Better than using the loop for this is to use the lapply or replicate functions. Something like:

list_ptf <- replicate(1000, data.frame( ... ), simplify=FALSE )
Greg Snow
  • 48,497
  • 6
  • 83
  • 110