I'm using a For loop to create 100 datasets according to some specifications. My end goal is to have 1 dataset containing each iterated dataset (i.e., dataset 1 through 100).
My current solution is inelegant. I export each individual data frame (called Dataset) to a csv then merge them outside R. With each iteration i of the For loop, my data frame is overwritten.
Trackfile=1:100
for (i in Trackfile){
d.cor <- .10 # Desired correlation
Dataset <- as.data.frame(mvrnorm(20, mu = c(0,0),
Sigma = matrix(c(1,d.cor,d.cor,1), ncol = 2),
empirical = TRUE))
write.csv(Dataset, paste0("C:/",d.cor," ",i,".csv"))
}
I believe the solution is to dynamically name the data frame according to the iteration (i) such that the data frames are named dataset1, dataset2...dataset100, then merge them. But I've struggled to find a solution for dynamically naming data frames embedded in a For loop. I'm a novice in R, please help!
forloop. Alternatively, you can create a list/array/vector of dataframes.