0

I have a character vector in R, (named q2) where each observation is a person's response to an open-ended question on a survey. Briefly, how do I tell R to save each person's response as a text file? E.g., "file01.txt" "file02.txt" etc. for all cases?

Is there a more efficient or better way than this?

for(i in 1:length(q2)) {
    filename <- paste("file",i,".txt", sep="")
    writeLines(as.character(q2[i]), con=filename) 
}
zero323
  • 322,348
  • 103
  • 959
  • 935
tcarpenter
  • 265
  • 1
  • 2
  • 11
  • Hi and welcome to stackoverflow! Please read [about Stackoverflow](http://stackoverflow.com/about), [what to ask](http://stackoverflow.com/help/on-topic) and [how to ask](http://meta.stackoverflow.com/help/how-to-ask). You are much more likely to receive a helpful answer if you provide a [minimal, reproducible data set](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) together with the code you have tried. Thanks! – Henrik Oct 13 '13 at 22:21
  • PS: is there a more efficient or better way than this? for(i in 1:length(q2)) { filename <- paste("file",i,".txt", sep="") writeLines(as.character(q2[i]), con=filename) } – tcarpenter Oct 13 '13 at 22:22
  • 1
    I would question the efficiency of storing each person's response in a separate text file. I/O operations are more time consuming than inefficiency of the for loop AFAIK. – Ramnath Oct 14 '13 at 00:17
  • This was to work with external software that requires separate text files. However, the tm() package can now do what my function above does. – tcarpenter Dec 08 '13 at 23:03

2 Answers2

2

So the "general rule" in R is to avoid writing loops, but you don't need to follow that rule blindly. The reason to avoid loops is that loops are inefficient because iterating through a loop in R requires a lot of interpreted machinery. However, this only really matters if the actual content of the loop can be executed very quickly, in which case the iteration will become a bottleneck. For example, you would never want to add two vectors together by writing

for(i in 1:length(x))
   z[i]<-x[i]+y[i]

However, in your case, the body of the loop requires opening a file and writing to it, and these operations will take much longer than the iteration. So in this case, using a loop is fine, and there is no reason to try and optimize this any further.

mrip
  • 14,913
  • 4
  • 40
  • 58
2

One thing you could have done is remove the creation of filenames from the loop, since paste can be vectorized:

    #simulation of your data
    q2 <- sample(c("yes", "no", "maybe", "idk", "nevermind"), 20, T)

    #names of potential ".txt"s. ("formatC" is used to add a leading "0")
    filenames <- paste("file", formatC(1:length(q2), digits = 1, flag = "0"), ".txt", sep = "")

    #set working directory to write the files
    setwd("C:/Desktop/myfiles")

    #the loop
    for(i in 1:length(q2))
     {
      write(q2[i], file = filenames[i])
     }
alexis_laz
  • 12,884
  • 4
  • 27
  • 37