John
John

Reputation: 61

Exporting text with data in a text file From R

I have the following problem. I wish to export data in a text file. But I do need to add some header inside the loop. For example :

for (i in 1:nrow(data)) {
 write.table("SomeText -- i -- some text " ..)
  for (j in 1:somevalue) {
 places data in the text file
  }
}

the "i" in the header is important, it indicate the analysis stage and is crucial for my other software uses. It has to be placed between text. Also, the text in the header should not have those " " to deliminate it. Any idea? Thanks! At first, I was using Tcl to do the job but the data are too big (over 2G) so Tcl doesn't work on this.

Upvotes: 0

Views: 577

Answers (2)

Federico Manigrasso
Federico Manigrasso

Reputation: 1200

I am trying to understand what you mean, sorry If you don't find this answer proper. I get you want add one column with some ID for each row. You can in my opinion avoid the for loop. Here a working example

data(iris)
df<-iris
head(df)
nrow=dim(df)[1]
i<-1:nrow
df<-data.frame(id=paste("SomeText --", i ,"-- some text ",sep=""),df)
head(df)
write.table(df,file="file.csv",sep=",",row.names=FALSE,quote=FALSE)

quote=FALSE prevent R to add " " in string fields

Upvotes: 1

greengrass62
greengrass62

Reputation: 986

write.table(paste("SomeText --", i ,"-- some text "))

Upvotes: 1

Related Questions