R - 动态变量名和嵌套循环
R - Dynamic Variable Names and Nested Loop
我需要运行一段代码15次。
# Number of Successes for y rolls -- rolls = 1
Num.Dice <- 1 # number of dice
x <- 1 # Initialize While Loop
a1 <- c() # create empty vector
while(x < 100000) {
a1[(x:x)] <- sum(sample(1:6, Num.Dice, replace=TRUE)>=5)
x <- x + 1
}
# Number of Successes for y rolls -- rolls = 2
Num.Dice <- 2 # number of dice
x <- 1 # Initialize While Loop
a2 <- c() # create empty vector
while(x < 100000) {
a2[(x:x)] <- sum(sample(1:6, Num.Dice, replace=TRUE)>=5) # load number of dice above 5/6
x <- x + 1 # step loop
}
...
这将创建 15 个独立的向量 a1:a15。 monte carlo 模拟(100000 次重复)对每个骰子 (Num.Dice) 的数量进行建模,每个骰子的数量超过 5 个或 6 个。每卷中的骰子数量随着每个向量的增加而增加(1-15 :: a1:15 -> 示例提供了 a1 和 a2 的线性规划代码)。
我想使用一个循环来为每个向量使用一个动态变量名来做到这一点。在循环和动态变量名之间我不知道从哪里开始。
我不是 100% 确定这就是您的想法,但是下面的代码 returns a data.frame
称为 final_df
,其中包含列中的所有采样向量。
一切都在 base R 中完成。请不要为了可读性而将 while 循环增加到 10。相应地改变它。
请看看这是不是你想要的。
# number of runs
Nruns <- 15
# output structure as a list for convenience
out_structure <- vector( "list", Nruns )
for ( i_dice in seq_len( Nruns ) ) {
# Number of Successes for y rolls -- rolls = 1
Num.Dice <- i_dice # number of dice
x <- 1 # Initialize While Loop
a <- c() # create empty vector
while(x < 10) {
# this temporary saves the vector "a" in each run
a[(x:x)] <- sum(sample(1:6, i_dice, replace=TRUE)>=5)
x <- x + 1
}
# and then assigns it to the output structure
out_structure[[i_dice]] <- a
}
# convert everything in a data.frame for dicing and slicing
final_df <- as.data.frame(out_structure,
col.names = paste0("a", seq_len( Nruns )))
final_df
#> a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15
#> 1 0 1 3 1 2 2 0 3 4 3 6 1 5 2 6
#> 2 0 0 1 1 1 4 3 3 5 0 5 4 4 5 3
#> 3 1 0 1 1 2 2 2 2 4 5 4 5 7 4 7
#> 4 0 0 1 1 4 0 3 3 2 4 4 3 2 6 4
#> 5 0 1 1 0 0 1 3 1 3 3 3 4 4 7 3
#> 6 0 0 2 2 1 1 3 2 5 3 5 7 6 6 6
#> 7 0 0 2 2 3 3 2 4 2 1 3 5 5 5 4
#> 8 0 1 1 2 0 2 3 1 2 5 6 6 3 8 4
#> 9 1 1 1 1 2 2 3 1 3 5 2 5 5 5 6
由 reprex package (v1.0.0)
于 2021 年 3 月 18 日创建
我需要运行一段代码15次。
# Number of Successes for y rolls -- rolls = 1
Num.Dice <- 1 # number of dice
x <- 1 # Initialize While Loop
a1 <- c() # create empty vector
while(x < 100000) {
a1[(x:x)] <- sum(sample(1:6, Num.Dice, replace=TRUE)>=5)
x <- x + 1
}
# Number of Successes for y rolls -- rolls = 2
Num.Dice <- 2 # number of dice
x <- 1 # Initialize While Loop
a2 <- c() # create empty vector
while(x < 100000) {
a2[(x:x)] <- sum(sample(1:6, Num.Dice, replace=TRUE)>=5) # load number of dice above 5/6
x <- x + 1 # step loop
}
...
这将创建 15 个独立的向量 a1:a15。 monte carlo 模拟(100000 次重复)对每个骰子 (Num.Dice) 的数量进行建模,每个骰子的数量超过 5 个或 6 个。每卷中的骰子数量随着每个向量的增加而增加(1-15 :: a1:15 -> 示例提供了 a1 和 a2 的线性规划代码)。
我想使用一个循环来为每个向量使用一个动态变量名来做到这一点。在循环和动态变量名之间我不知道从哪里开始。
我不是 100% 确定这就是您的想法,但是下面的代码 returns a data.frame
称为 final_df
,其中包含列中的所有采样向量。
一切都在 base R 中完成。请不要为了可读性而将 while 循环增加到 10。相应地改变它。
请看看这是不是你想要的。
# number of runs
Nruns <- 15
# output structure as a list for convenience
out_structure <- vector( "list", Nruns )
for ( i_dice in seq_len( Nruns ) ) {
# Number of Successes for y rolls -- rolls = 1
Num.Dice <- i_dice # number of dice
x <- 1 # Initialize While Loop
a <- c() # create empty vector
while(x < 10) {
# this temporary saves the vector "a" in each run
a[(x:x)] <- sum(sample(1:6, i_dice, replace=TRUE)>=5)
x <- x + 1
}
# and then assigns it to the output structure
out_structure[[i_dice]] <- a
}
# convert everything in a data.frame for dicing and slicing
final_df <- as.data.frame(out_structure,
col.names = paste0("a", seq_len( Nruns )))
final_df
#> a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15
#> 1 0 1 3 1 2 2 0 3 4 3 6 1 5 2 6
#> 2 0 0 1 1 1 4 3 3 5 0 5 4 4 5 3
#> 3 1 0 1 1 2 2 2 2 4 5 4 5 7 4 7
#> 4 0 0 1 1 4 0 3 3 2 4 4 3 2 6 4
#> 5 0 1 1 0 0 1 3 1 3 3 3 4 4 7 3
#> 6 0 0 2 2 1 1 3 2 5 3 5 7 6 6 6
#> 7 0 0 2 2 3 3 2 4 2 1 3 5 5 5 4
#> 8 0 1 1 2 0 2 3 1 2 5 6 6 3 8 4
#> 9 1 1 1 1 2 2 3 1 3 5 2 5 5 5 6
由 reprex package (v1.0.0)
于 2021 年 3 月 18 日创建