R:包含列表和数据框的循环
R: Loops Containing Lists and Data Frames
我正在使用 R 编程语言。
我为一个随机采样整数(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
现在,我想向此数据框添加 5 个新列,其中包含“n”个随机整数。比如第一行:
n_1_i = 5 :我想创建一个名为“cond_1”的专栏,这样 cond_1 <- sample(1:10, 5, replace=F)
n_2_i = 9 :我想创建一个名为“cond_1”的专栏,这样 cond_2 <- sample(1:10, 9, replace=F)
n_3_i = 9 :我想创建一个名为“cond_1”的专栏,这样 cond_3 <- sample(1:10, 7, replace=F)
n_4_i = 9 :我想创建一个名为“cond_1”的专栏,这样 cond_4 <- sample(1:10, 5, replace=F)
n_5_i = 9 :我想创建一个名为“cond_1”的专栏,这样 cond_5 <- sample(1:10, 5, replace=F)
这将对 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)
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))
但这给出了以下错误:(我怀疑这个错误是因为我试图将列表与数据框混合?)
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
最后,我想制作这样的东西(我已经展示了第一行的例子):
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
有人可以告诉我怎么做吗?
谢谢!
下面是我将如何重写代码。我们以长格式开始,以跳过所有 copy/pasting 相似命名的列。如果您想更改每次迭代的绘制次数,这也很容易概括。然后(如果需要)在最后转得更宽。 (尽管我建议将其保存为长格式以便使用它...)
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
我正在使用 R 编程语言。
我为一个随机采样整数(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
现在,我想向此数据框添加 5 个新列,其中包含“n”个随机整数。比如第一行:
n_1_i = 5 :我想创建一个名为“cond_1”的专栏,这样
cond_1 <- sample(1:10, 5, replace=F)
n_2_i = 9 :我想创建一个名为“cond_1”的专栏,这样
cond_2 <- sample(1:10, 9, replace=F)
n_3_i = 9 :我想创建一个名为“cond_1”的专栏,这样
cond_3 <- sample(1:10, 7, replace=F)
n_4_i = 9 :我想创建一个名为“cond_1”的专栏,这样
cond_4 <- sample(1:10, 5, replace=F)
n_5_i = 9 :我想创建一个名为“cond_1”的专栏,这样
cond_5 <- sample(1:10, 5, replace=F)
这将对 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)
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))
但这给出了以下错误:(我怀疑这个错误是因为我试图将列表与数据框混合?)
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
最后,我想制作这样的东西(我已经展示了第一行的例子):
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
有人可以告诉我怎么做吗?
谢谢!
下面是我将如何重写代码。我们以长格式开始,以跳过所有 copy/pasting 相似命名的列。如果您想更改每次迭代的绘制次数,这也很容易概括。然后(如果需要)在最后转得更宽。 (尽管我建议将其保存为长格式以便使用它...)
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