0

I have a loop for a data frame construction, and I would like to write all small pieces at each iteration in a csv file. Something like a rbind() but in a file...

I have seen sink() like that

exemple :

sink("resultat/output.txt")
df.total<-NULL
for(i in 1:length(list2.1)){
  if(i%%100==0){print(i)}
  tps<-as.data.frame(list2.1[i])
  tps<-cbind(tps,as.data.frame(list2.2[i]))
  colnames(tps)<-c("slope","echange")
  tps$Run<-rep(data$run_nb[i],length(tps$slope))
  tps$coefSlope<-rep(data$coef_Slope_i[i],length(tps$slope))
  tps$coefDist<-rep(data$coef_dist_i[i],length(tps$slope))
  sink(tps)
  df.total<-rbind(df.total,tps)
}
sink()
write.csv(df.total,"resultat/df_total.csv")

but I don't think it work for my case ... any suggestions

delaye
  • 1,357
  • 27
  • 45
  • Consider editing your question a bit. Show us your problem with a [minimal example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Your example is too long, not focused on your problem and we are missing the data. – alko989 Jul 28 '14 at 21:59

1 Answers1

2

You can use sink (which redirects R output to a connection) along with print

df <- data.frame(a = 0:1, b = 1:2)
sink("output.txt")
for(i in 1:10) {
    print(df[i+2, ] <- c(sum(df$a), tail(df$b, 1) + 1))
    ## Or, to save the whole data.frame each time: print(df)
}
sink()

Another option is to use cat

df <- data.frame(a = 0:1, b = 1:2)
for(i in 1:10) {
    cat(df[i+2, ] <- c(sum(df$a), tail(df$b, 1) + 1), "\n", file="output.txt" append=TRUE)
}

You can also use write.table to save the whole data.frame

df <- data.frame(a = 0:1, b = 1:2)
for(i in 1:10) {
    df[i+2, ] <- c(sum(df$a), tail(df$b, 1) + 1)
    write.table(df, file="output.txt", append=TRUE)
}

If you set append = FALSE only the last iteration will be saved.

alko989
  • 7,688
  • 5
  • 39
  • 62