将几个循环的结果组合在一起

Combining the Results of Several Loops Together

我写了下面的代码生成一个随机数,从某个常数中减去这个随机数,记录这个结果——然后重复这个过程 100 次:

# 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)

要对2个随机数和3个随机数进行此操作 - 上面的代码只需要稍微修改一下:

# 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)

我的问题:我想重复这个一般过程 20 次并将所有结果存储在一个数据框中。例如(注意:实际数据框会有 20 对这样的列):

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

然后制作两个均值文件(注意:这两个文件中的每一个也有 20 行):

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) ) )

有人可以告诉我怎么做吗?

你的初始 objective 可以这样简化:

results <- list()
for (i in seq_len(100)) {
  #Samples from 1 to 20 numbers, averages them
  a <- unlist(lapply(seq_len(20), function(x) mean(rnorm(x, 10, 2))))
  #Creates names for this vector
  names(a) <- paste0(rep("number_i_", 20), 1:20)
  #differences
  b <- 10-a
  #and it's names
  names(b) <- paste0(rep("diff_i_", 20), 1:20)
  #creating 40c df (there are better structures for this specially if the final outcome is to separate them)
  c <- as.data.frame(cbind(rbind(a), rbind(b)))
#storing in list
  results[[i]] <- c
}

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

还有更优雅的写法,但足以让您到达那里。

你上一节中的格式对你想要实现的目标没有意义。如果是为每个采样数创建均值汇总,像这样:

mockfdf <- data.frame(nsamp = 1:20, meanmeans = rnorm(20))#summarized means go here

mockddf <- data.frame(nsamp = 1:20, diffmeans = rnorm(20))#summarized means go here

然后您可以轻松地分离数据帧的差异和均值,并通过为每个数据帧使用单独的数据帧来更好地处理它们。