【问题标题】:writing files in R loops在 R 循环中写入文件
【发布时间】:2020-05-02 08:47:57
【问题描述】:

我正在尝试在 R 中的循环中编写一个文件。我想在我们通过循环时附加该文件。我试过 append=true 但没有用。我已经详细解释了问题here。任何想法?感谢您的帮助:

#Initilaizing 2*2 array
Array1 <- array(0,c(2,2))

for(i in 1:5)
{
#Create 4 random numbers  
Random1 <- runif(1, min=0, max=1)
Random2 <- runif(1, min=0, max=1)
Random3 <- runif(1, min=0, max=1)
Random4 <- runif(1, min=0, max=1)

#Assign Random numbers to the array
Array1[1,1] <- Random1
Array1[1,2] <- Random2
Array1[2,1] <- Random3
Array1[2,2] <- Random4

#*****This is the Question*******
# I want to keep the history for Array1 through the 5 loops by writing the array in a file.
# and appending the file as we go through the loop
# I tried write.csv with Append=true but didn't work
# How can I do this?

}

【问题讨论】:

  • 我在你的代码中没有看到任何write.csv
  • 我试过 write.csv(Array1, file= "OUT1.csv", append=true) 但没有成功。
  • @movassat 它应该是 append=TRUE 。在 R 中,布尔对象可以取两个值 TRUEFALSE
  • write.csv(Array1, file= "OUT4.csv", append=TRUE) 。我在循环中尝试了这个,仍然只写下最后一次迭代结果
  • 向我们展示代码(全部)或工作示例。

标签: r loops csv


【解决方案1】:

write.table() 在这里效果更好,因为write.csv() 限制性更强,以确保始终写入有效的 CSV 文件。

write.table()append = TRUEcol.names = FALSE (为了抑制列名被重复写入文件)应该可以解决问题。如果希望分隔符为逗号,为了符合csv规范,可以设置为sep = ","

这可能是这样的:

Array1 <- array(0,c(2,2))

for(i in 1:5)
{
  #Create 4 random numbers  
  Random1 <- runif(1, min=0, max=1)
  Random2 <- runif(1, min=0, max=1)
  Random3 <- runif(1, min=0, max=1)
  Random4 <- runif(1, min=0, max=1)

  #Assign Random numbers to the array
  Array1[1,1] <- Random1
  Array1[1,2] <- Random2
  Array1[2,1] <- Random3
  Array1[2,2] <- Random4

  write.table(Array1,
              sep = ",",
              file = "OUT1.csv", 
              append = TRUE,
              col.names = FALSE,
              row.names = FALSE)
}

如果你想避免使用 write.table() 为什么不在 R 中绑定数组,然后同时写入所有内容:

out_array <- array(numeric(), c(0,2))
for(i in 1:5)
{
  Array1 <- array(dim = c(2,2))
  #Create 4 random numbers  
  Random1 <- runif(1, min=0, max=1)
  Random2 <- runif(1, min=0, max=1)
  Random3 <- runif(1, min=0, max=1)
  Random4 <- runif(1, min=0, max=1)

  #Assign Random numbers to the array
  Array1[1,1] <- Random1
  Array1[1,2] <- Random2
  Array1[2,1] <- Random3
  Array1[2,2] <- Random4

  out_array <- rbind(out_array, Array1)
}

write.csv(out_array, "OUT2.csv")

最后,对于函数式编程爱好者来说,一个解决方案可以在一个管道链中完成所有事情并使用 purrr 的映射:

library(dplyr)
library(purrr)
map(1:5, function(repeats) {
    Array1 <- array(dim = c(2,2))
    #Create 4 random numbers  
    Random1 <- runif(1, min=0, max=1)
    Random2 <- runif(1, min=0, max=1)
    Random3 <- runif(1, min=0, max=1)
    Random4 <- runif(1, min=0, max=1)

    #Assign Random numbers to the array
    Array1[1,1] <- Random1
    Array1[1,2] <- Random2
    Array1[2,1] <- Random3
    Array1[2,2] <- Random4
    Array1
}) %>% 
  {do.call(rbind, .)} %>% 
  write.csv("OUT3.csv")

【讨论】:

  • 太棒了!你介意投票/标记我的答案是正确的吗?!
猜你喜欢
  • 2016-06-22
  • 1970-01-01
  • 2015-09-25
  • 2012-06-27
  • 1970-01-01
  • 2017-08-22
  • 2012-08-20
  • 1970-01-01
  • 2019-07-30
相关资源
最近更新 更多