vendredi 30 novembre 2018

r - generate multiple files from randomizing a data frame

I need to generate and save multiple files from the randomization of a data frame. The original data frames are daily weather data for several years. I need to generate files that are random reorganizations of the years but keeping the year sequence.

I have developed a simple code for randomizing years, but I am having trouble to repeat randomization and save each output randomized data frame as a separate file.

This is what I have thus far:

# Create example data frame
df <- data.frame(x=c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,8,8))
df$y <- c(4,8,9,1,1,5,8,8,3,2,0,9,4,4,7,3,5,5,2,4,6,6)
df$z <- c("A","A","A","B","B","B","C","C","C","D","D","D","F","F","F","G","G","G","H","H","I","I")

set.seed(30)

# Split data frame based on info in one column (i.e. df$x) and store in a list 
dt_list <- split(df, f = df$x)

# RANDOMIZE data list -- Create a new index and change the order of dt_list
# SAVE the result to "random list" (i.e. 'rd_list')

rd_list <- dt_list[sample(1:length(dt_list), length(dt_list))]

# Put back together data in the order established in 'rd_list' 
rd_data <- do.call(rbind, rd_list)

This randomizes the data frame just as I need, but I don't know how to "save & repeat" so I get multiple files, let's say about 20, named as the original and a sequential numeration (e.g. df_1, df_2 ...).

Also, being random samples, it's possible to get repetitions. Is there any way to automatically discard repeated files?

Thanks!




Aucun commentaire:

Enregistrer un commentaire