movassat
movassat

Reputation: 13

writing files in R loops

I am trying to write a file in a loop in R. I want to append the file as we go through the loop. I tried append=true but didn't work. I have explained the problem with details here. Any idea? thanks for your help:

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

}

Upvotes: 1

Views: 67

Answers (1)

Till
Till

Reputation: 6628

write.table() works better here, since write.csv() is more restrictive, in order to ensure always writing valid CSV files.

write.table() with append = TRUE and col.names = FALSE (in order to suppress column names being written into the file repeatedly) should do the trick. If you want the separator to be a comma, in order to comply with the csv specification, you can set it with sep = ",".

This is what this could look like:

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

If you want to avoid using write.table() why not bind the arrays in R and then write everything at the same time:

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

And finally, for the functional programming enthusiasts, a solution that does everything in one pipe chain and with purrr's map:

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

Upvotes: 1

Related Questions