如何将所有重复的循环结果保存在数据帧中的 R 中

How to save all repeated loop results in R in a dataframe

我创建了一个用于引导重复测量的循环,每个采样日选择一个 运行dom 测量。循环应该 运行 100 次。

这是我的输入数据框的样子:

  date        spot treatment   site  N2O.units N2O.pv N2O.flux t.air
   <chr>      <int> <chr>       <chr> <chr>     <chr>     <dbl> <dbl>
 1 2019-06-21    31 min 5, bare LV    mg        **       0.0002  16.5
 2 2019-06-21    46 min 5, bare LV    mg        *        0       11.1
 3 2019-07-05    31 min 5, bare LV    mg        **       0.0001  17.2
 4 2019-07-05    36 min 5, bare LV    mg        **       0.0004  18.4
 5 2019-07-05    46 min 5, bare LV    mg        **       0.0001  17.2
 6 2019-07-19    26 min 5, bare LV    mg        ***      0       29.4
 7 2019-07-19    31 min 5, bare LV    mg        ***      0.0002  29.4
 8 2019-07-19    36 min 5, bare LV    mg        **       0.0002  19.9
 9 2019-08-02    26 min 5, bare LV    mg        **       0       26.9
10 2019-08-02    36 min 5, bare LV    mg        **       0.0001  33.9
# ... with 44 more rows

首先我创建了一个列表

lst <- list()

然后我 运行 循环(示例中只有五次迭代)

for(i in 1:5)

 {
  LV %>%
    group_by(date) %>%
    sample_n(1) -> result
  print(result$N2O.flux)
  lst[i]<-result$N2O.flux
}

导致此输出,实际上非常好,也是我想要的

 [1]  2e-04  1e-04  2e-04  1e-04  0e+00  0e+00  0e+00  0e+00  0e+00  0e+00  0e+00  0e+00  0e+00
[14]  0e+00  0e+00  0e+00  0e+00  0e+00  1e-04  0e+00  0e+00 -1e-04  0e+00  0e+00  0e+00
 [1]  0e+00  1e-04  0e+00  0e+00  1e-04  0e+00  0e+00  0e+00  0e+00  1e-04  0e+00  0e+00  0e+00
[14] -2e-04  0e+00  0e+00  0e+00  0e+00  0e+00  0e+00  0e+00  0e+00  1e-04  0e+00  0e+00
 [1]  0e+00  4e-04  2e-04  0e+00  0e+00  0e+00  0e+00  0e+00  0e+00  1e-04  0e+00  0e+00  0e+00
[14]  0e+00  1e-04  0e+00  0e+00  0e+00  1e-04  0e+00  0e+00  0e+00 -1e-04  0e+00  0e+00
 [1]  2e-04  1e-04  0e+00  1e-04  1e-04  1e-04  0e+00  0e+00  0e+00  0e+00  0e+00  0e+00  0e+00
[14] -2e-04  0e+00  0e+00  0e+00  0e+00  1e-04  0e+00  0e+00 -1e-04  0e+00  0e+00  0e+00
 [1]  0e+00  1e-04  2e-04  0e+00  0e+00  0e+00  0e+00  0e+00  0e+00  0e+00  0e+00  0e+00  0e+00
[14]  0e+00  1e-04  0e+00  0e+00  0e+00  0e+00  0e+00  0e+00  0e+00 -1e-04  0e+00  0e+00

但是,我得到的只是最后一个值

 str(lst)
List of 5
 $ : num 0
 $ : num 0
 $ : num 2e-04
 $ : num 0
 $ : num 2e-04

write.table(as.data.frame(lst),file="mylist2.csv", quote=F,sep=",",row.names=F) 编写 .csv 时看起来像那样(非常糟糕)

X0 X0.1 X2e.04 X0.2 X2e04.1
0 0 2.00e-04 0 2.00e-04

所以,我的问题是:

  1. 如何在列表中保存每个 运行 的所有循环结果?
  2. 如何为各种循环 运行 分配数字,以便可以将结果打印在整齐的列中,并将数值作为 .csv 中的标识符以供进一步处理?在我的幻想中,输出看起来像:
1 2 3 4 5 ...etc 100
0 0 2.00e-04 0 2.00e-04 ... -1e-04
2.00e-04 0 2.00e-04 0 2.00e-04 ... -1e-04
-1e-04 0 4 0 2.00e-04 ... -1e-04
0 2.00e-04 2.00e-04 0 -1e-04 ... -1e-04
-1e-04 0 2.00e-04 0 2.00e-04 ... -1e-04

等等(25行)

您可能注意到了警告

Warning message:
In lst[i] <- result$N2O.flux :
  number of items to replace is not a multiple of replacement length

通过说 lst[i] <- result$N2O.flux,您试图添加一个矢量,即具有多个元素的对象作为子列表到列表 lst。在这种情况下,分配的对象被修剪到第一个元素,这在警告中有说明。

因此,您可以 list() 向量,通过说

来获取要分配的单个对象
lst[i] <- list(result$N2O.flux)

或通过说

访问子列表并向其添加元素
lst[[i]] <- result$N2O.flux

或者,您可以尝试一种不同的方法,它会为您提供一个矩阵,行中包含日期,列中绘制:

set.seed(42)
res <- replicate(n=5, sapply(split(LV$N2O.flux, LV$date), sample, 1))
res
#             [,1]  [,2]  [,3]  [,4]  [,5]
# 2019-06-21 2e-04 0e+00 0e+00 2e-04 0e+00
# 2019-07-05 1e-04 4e-04 1e-04 1e-04 4e-04
# 2019-07-19 0e+00 2e-04 2e-04 2e-04 2e-04
# 2019-08-02 0e+00 0e+00 1e-04 1e-04 0e+00

write.csv(res, file='test.csv')

这给了你


数据:

LV <- structure(list(date = c("2019-06-21", "2019-06-21", "2019-07-05", 
"2019-07-05", "2019-07-05", "2019-07-19", "2019-07-19", "2019-07-19", 
"2019-08-02", "2019-08-02"), spot = c(31L, 46L, 31L, 36L, 46L, 
26L, 31L, 36L, 26L, 36L), treatment = c("min 5, bare", "min 5, bare", 
"min 5, bare", "min 5, bare", "min 5, bare", "min 5, bare", "min 5, bare", 
"min 5, bare", "min 5, bare", "min 5, bare"), site = c("LV", 
"LV", "LV", "LV", "LV", "LV", "LV", "LV", "LV", "LV"), N2O.units = c("mg", 
"mg", "mg", "mg", "mg", "mg", "mg", "mg", "mg", "mg"), N2O.pv = c("**", 
"*", "**", "**", "**", "***", "***", "**", "**", "**"), N2O.flux = c(2e-04, 
0, 1e-04, 4e-04, 1e-04, 0, 2e-04, 2e-04, 0, 1e-04), t.air = c(16.5, 
11.1, 17.2, 18.4, 17.2, 29.4, 29.4, 19.9, 26.9, 33.9)), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10"))

如果您想提取 csv,最好直接用您的结果制作数据框。一个简单的方法是:

all_iter <- data.frame()
for(i in 1:5) {
  result <- LV %>%group_by(date) %>%sample_n(1)
  print(result$N2O.flux)
  all_iter <- all_iter %>% rbind(c(i, result$N2O.flux))
}

all_iter的每一行对应一个迭代。第一列对应于迭代次数,其余列对应于该迭代的值。

然后您可以使用 :

导出 all_iter
write.csv(all_iter, 'all_iterations_result.csv', quote=F,sep=",",row.names=F)