【问题标题】:How to save all repeated loop results in R in a dataframe如何将所有重复的循环结果保存在数据框中的R中
【发布时间】:2021-05-27 08:31:13
【问题描述】:

我创建了一个循环来引导重复测量,每个采样日选择一个随机测量。循环应该运行 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行)

【问题讨论】:

  • 您在寻找append() 命令吗? lst &lt;- append(lst, list(result$N2O.flux))

标签: r loops repeat


【解决方案1】:

你可能注意到了警告

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

通过说lst[i] &lt;- 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"))

【讨论】:

  • 您的替代方法非常出色(而且简单)。这正是我所希望的。
【解决方案2】:

当您想提取 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    • 2019-10-10
    • 2017-04-16
    • 2020-03-15
    • 1970-01-01
    相关资源
    最近更新 更多