简体   繁体   English

R:包含列表和数据框的循环

[英]R: Loops Containing Lists and Data Frames

I am working with the R programming language.我正在使用 R 编程语言。

I wrote the following code for a loop that randomly sample integers (between 1-10) 5 times:我为一个随机采样整数(1-10 之间)5 次的循环编写了以下代码:

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.现在,我想向这个包含“n”个随机整数的数据框添加 5 个新列。 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_1_i = 5:我想创建一个名为“cond_1”的列,这样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_2_i = 9:我想创建一个名为“cond_1”的列,这样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_3_i = 9:我想创建一个名为“cond_1”的列,这样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_4_i = 9:我想制作一个名为“cond_1”的列,这样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) n_5_i = 9:我想创建一个名为“cond_1”的列,这样cond_5 <- sample(1:10, 5, replace=F)

This would be repeated for each of the 5 rows.这将对 5 行中的每一行重复。

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!谢谢!

Here's how I would rewrite the code.下面是我将如何重写代码。 We start in long format to skip all copy/pasting of similarly named columns.我们以长格式开始,以跳过所有复制/粘贴类似名称的列。 This also generalizes easily if you want to change the number of draws per iteration.如果您想更改每次迭代的绘制次数,这也很容易概括。 Then (if needed) pivot wider at the end.然后(如果需要)最后加宽 pivot。 (Though I'd recommend keeping it in long format for working with it...) (尽管我建议将其保存为长格式以便使用它......)

library(dplyr)
library(purrr)
library(tidyr)

n_iter = 5
n_i = 5
set.seed(47)  ## for reproducibility - remove if you want new random numbers
results = expand.grid(iter = seq_len(n_iter), i = seq_len(n_i)) %>% 
  mutate(n = sample(1:10, size = n_iter * n_i, replace = TRUE))
results   
#    iter i  n
# 1     1 1  9
# 2     2 1  2
# 3     3 1  4
# 4     4 1  1
# 5     5 1 10
# 6     1 2  7
# 7     2 2  6
# ...

results = results %>%
  mutate(cond = map(n, ~sample(1:10, size = .x, replace = FALSE)))

results
#    iter i  n                          cond
# 1     1 1  9    5, 10, 8, 1, 9, 2, 7, 3, 4
# 2     2 1  2                          7, 8
# 3     3 1  4                    5, 8, 3, 1
# 4     4 1  1                             8
# 5     5 1 10 10, 2, 9, 3, 5, 8, 4, 6, 7, 1
# 6     1 2  7          7, 2, 10, 8, 6, 4, 3
# 7     2 2  6             5, 1, 7, 8, 6, 10
# ...

results = results %>%
  pivot_wider(
    id_cols = iter,
    values_from = c("n", "cond"),
    names_from = "i",
    names_glue = "{.value}_{i}"
)
results
# # A tibble: 5 × 11
#    iter   n_1   n_2   n_3   n_4   n_5 cond_1     cond_2    cond_3    cond_4     cond_5   
#   <int> <int> <int> <int> <int> <int> <list>     <list>    <list>    <list>     <list>   
# 1     1     9     7     8     5     3 <int [9]>  <int [7]> <int [8]> <int [5]>  <int [3]>
# 2     2     2     6     8    10     6 <int [2]>  <int [6]> <int [8]> <int [10]> <int [6]>
# 3     3     4     9     5     5     1 <int [4]>  <int [9]> <int [5]> <int [5]>  <int [1]>
# 4     4     1     6     4     2     5 <int [1]>  <int [6]> <int [4]> <int [2]>  <int [5]>
# 5     5    10     6     9     5     1 <int [10]> <int [6]> <int [9]> <int [5]>  <int [1]>

print.data.frame(results)
#   iter n_1 n_2 n_3 n_4 n_5                        cond_1                     cond_2                    cond_3                        cond_4
# 1    1   9   7   8   5   3    5, 10, 8, 1, 9, 2, 7, 3, 4       7, 2, 10, 8, 6, 4, 3   9, 6, 8, 4, 5, 10, 3, 1                 9, 5, 6, 7, 3
# 2    2   2   6   8  10   6                          7, 8          5, 1, 7, 8, 6, 10    8, 2, 7, 5, 4, 1, 9, 3 9, 5, 3, 6, 2, 1, 7, 10, 8, 4
# 3    3   4   9   5   5   1                    5, 8, 3, 1 5, 1, 4, 3, 6, 10, 7, 8, 9            9, 3, 10, 1, 4                8, 10, 1, 7, 6
# 4    4   1   6   4   2   5                             8           7, 3, 2, 1, 6, 4                3, 1, 2, 5                          1, 7
# 5    5  10   6   9   5   1 10, 2, 9, 3, 5, 8, 4, 6, 7, 1           1, 9, 3, 7, 8, 4 8, 4, 6, 7, 5, 1, 3, 9, 2                7, 6, 5, 8, 10
#              cond_5
# 1           2, 5, 9
# 2 2, 1, 10, 3, 6, 7
# 3                 8
# 4    10, 6, 2, 8, 1
# 5                 6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM