vendredi 20 mai 2022

Combining the Results of Several Loops Together

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?




Aucun commentaire:

Enregistrer un commentaire