Home > Back-end >  Combining the Results of Several Loops Together
Combining the Results of Several Loops Together

Time:05-21

I wrote the following code that generates a single random number, subtracts this random number from some constant, records this result - and then repeats this process 100 times:

# 1 random number

results <- list()
for (i in 1:100) {

    iteration = i
    number_i_1 = mean(rnorm(1,10,2))
    difference_i_1 = 10 - number_i_1
 
    results_tmp = data.frame(iteration, number_i_1, difference_i_1)

    results[[i]] <- results_tmp
}

results_df_1 <- do.call(rbind.data.frame, results)

To do this for 2 random numbers and 3 random numbers - the above code only needs to be slightly modified:

# 2 random numbers

results <- list()
for (i in 1:100) {

    iteration = i
    number_i_2 = mean(rnorm(2,10,2))
    difference_i_2 = 10 - number_i_2
 
    results_tmp = data.frame( number_i_2, difference_i_2)

    results[[i]] <- results_tmp
}
results_df_2 <- do.call(rbind.data.frame, results)

# 3 random numbers

results <- list()
for (i in 1:100) {

    iteration = i
    number_i_3 = mean(rnorm(3,10,2))
    difference_i_3 = 10 - number_i_3
 
    results_tmp = data.frame( number_i_3, difference_i_3)

    results[[i]] <- results_tmp
}
results_df_3 <- do.call(rbind.data.frame, results)

My Question: I would like to repeat this general process 20 times and store all the results in a single data frame. For example (note: the actual data frame would have 20 pairs of such columns):

final_frame = cbind(results_df_1 , results_df_2, results_df_3)

  iteration number_i_1 difference_i_1 number_i_2 difference_i_2 number_i_3 difference_i_3
1         1  12.534059     -2.5340585   9.623655      0.3763455   9.327020     0.67298023
2         2   9.893728      0.1062721  10.135650     -0.1356502  10.037904    -0.03790384
3         3   8.895232      1.1047680   9.848402      0.1515981   7.588531     2.41146943
4         4  11.648550     -1.6485504   8.509288      1.4907120  10.294153    -0.29415334
5         5   9.045034      0.9549660   9.351834      0.6481655  11.084067    -1.08406691
6         6   9.230139      0.7698612   8.163164      1.8368356   7.846356     2.15364367

And then make two mean files (note: each of these two files would also have 20 rows):

mean_numbers = data_frame(iterations = c(1:3), mean_number = c(mean(final_frame$number_i_1),mean(final_frame$number_i_2), mean(final_frame$number_i_3) ) )

mean_differences = data_frame(iterations = c(1:3), mean_differences = c(mean(final_frame$difference_i_1),mean(final_frame$difference_i_1), mean(final_frame$difference_i_1) ) )

Can someone please show me how to do this?

CodePudding user response:

Your initial objective can be simplified like this:

results <- list()
for (i in seq_len(100)) {
  #Samples from 1 to 20 numbers, averages them
  a <- unlist(lapply(seq_len(20), function(x) mean(rnorm(x, 10, 2))))
  #Creates names for this vector
  names(a) <- paste0(rep("number_i_", 20), 1:20)
  #differences
  b <- 10-a
  #and it's names
  names(b) <- paste0(rep("diff_i_", 20), 1:20)
  #creating 40c df (there are better structures for this specially if the final outcome is to separate them)
  c <- as.data.frame(cbind(rbind(a), rbind(b)))
#storing in list
  results[[i]] <- c
}

results_df_3 <- do.call(rbind.data.frame, results)

There are even more elegant ways to write this but it will be enough for you to get there.

The format in your last section does not make sense to what you want to achieve. If it is to create a summary of the means for each number of samples taken, like this:

mockfdf <- data.frame(nsamp = 1:20, meanmeans = rnorm(20))#summarized means go here

mockddf <- data.frame(nsamp = 1:20, diffmeans = rnorm(20))#summarized means go here

Then you can easily separate the dataframes for differences and means and process them a lot better by using separate dataframes for each.

  • Related