1

I'm working in R and I would like to export a txt file putting in its name the value of a particular variable; I read about the command paste and it works perfectly here:

write.table(mydata,file=paste(cn,"data.txt"))

where cn is the value to put at the beginning of the file data.txt. I would like to automatically put this file in an output folder where I keep all the other results. I try to do something like this:

write.table(mydata,file=paste(cn,"./output/data.txt"))

But it doesn't work. Any suggestion?

Richard Erickson
  • 2,568
  • 8
  • 26
  • 39
F. De Leo
  • 37
  • 1
  • 5

1 Answers1

3

paste() just creates a string by concatenating the individual values and uses a space as default separator:

write.table(mydata, file = paste("./output/", cn ,"data.txt", sep = ""))

or with paste0(...), which is equivalent to paste(..., sep = ""):

write.table(mydata, file = paste0("./output/", cn ,"data.txt"))
Jaap
  • 81,064
  • 34
  • 182
  • 193
nevrome
  • 1,471
  • 1
  • 13
  • 28