jeudi 7 avril 2022

R: Loops Containing Lists and Data Frames

I am working with the R programming language.

I wrote the following code for a loop that randomly sample integers (between 1-10) 5 times:

results <- list()

for (i in 1:5) {

n_1_i = sample(1:10, 1, replace=F)
n_2_i = sample(1:10, 1, replace=F)
n_3_i = sample(1:10, 1, replace=F)
n_4_i = sample(1:10, 1, replace=F)
n_5_i = sample(1:10, 1, replace=F)


iteration = i
 
results_tmp = data_frame(iteration, n_1_i, n_2_i, n_3_i, n_4_i, n_5_i)

  results[[i]] <- results_tmp

}

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

head(results_df)

  iteration n_1_i n_2_i n_3_i n_4_i n_5_i
1         1     5     9     7     5     5
2         2     5     4    10     1    10
3         3    10     4     6     2     3
4         4     5     6     1     6     1
5         5    10     6     3     7     4

Now, I would like to add 5 new columns to this data frame that contain "n" number of random integers. For example, in the first row:

  • n_1_i = 5 : I would like to make a column called "cond_1" such that cond_1 <- sample(1:10, 5, replace=F)

  • n_2_i = 9 : I would like to make a column called "cond_1" such that cond_2 <- sample(1:10, 9, replace=F)

  • n_3_i = 9 : I would like to make a column called "cond_1" such that cond_3 <- sample(1:10, 7, replace=F)

  • n_4_i = 9 : I would like to make a column called "cond_1" such that cond_4 <- sample(1:10, 5, replace=F)

  • n_5_i = 9 : I would like to make a column called "cond_1" such that cond_5 <- sample(1:10, 5, replace=F)

This would be repeated for each of the 5 rows.

I tried to incorporate this logic into the existing loop:

results <- list()

for (i in 1:5) {

n_1_i = sample(1:10, 1, replace=F)
n_2_i = sample(1:10, 1, replace=F)
n_3_i = sample(1:10, 1, replace=F)
n_4_i = sample(1:10, 1, replace=F)
n_5_i = sample(1:10, 1, replace=F)

var_1_cond_i = sample(1:10, n_1_i, replace=F)
var_2_cond_i = sample(1:10, n_2_i, replace=F)
var_3_cond_i = sample(1:10, n_3_i, replace=F)
var_4_cond_i = sample(1:10, n_4_i, replace=F)
var_5_cond_i = sample(1:10, n_5_i, replace=F)

iteration = i
 
results_tmp = data_frame(iteration, n_1_i, n_2_i, n_3_i, n_4_i, n_5_i)

list_tmp = list(var_1_cond_i, var_2_cond_i, var_3_cond_i, var_4_cond_i, var_5_cond_i)


results[[i]] <- results_tmp

results_list[[i]] <- list_tmp

}

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

But this is giving the following error: (I suspect that this error is because I am trying to mix lists with data frames?)

Error in if (quote) args <- lapply(args, enquote) : 
  argument is not interpretable as logical
In addition: Warning message:
In if (quote) args <- lapply(args, enquote) :
  the condition has length > 1 and only the first element will be used

In the end, I would like to produce something like this (I have shown an example of the first row):

  iteration n_1_i n_2_i n_3_i n_4_i n_5_i var_1_cond_i              var_2_cond_i         var_3_cond_i var_4_cond_i var_5_cond_i
1         1     5     9     7     5     5    9 4 6 7 3 2  8  3  6  7  5 10  4  1 10  2  3  1  7  6  5    8 1 3 9 4    8 1 3 4 6

Can someone please show me how to do this?

Thanks!




Aucun commentaire:

Enregistrer un commentaire