user1815498
user1815498

Reputation: 1277

write.table to new directory

Is there any way to use write() and write.table() so that the output file is in a different directory than the working directory? It tried setting the path to the output file before the filename, and just get an error message.

Upvotes: 7

Views: 41064

Answers (2)

Señor O
Señor O

Reputation: 17412

If you're using windows, R will know to go outside the current directory if it sees C:/ first (presumably other mounted drives too). With Macs it will go outside the current wd if it sees /. So:

  1. Mac OS X:

    write.table(foo, file="/users/name/folder/filename.ext")
    
  2. Windows

    write.table(foo, file="C:/users/name/folder/filename.ext")
    

Always test to make sure you have the path right first!

list.files("C:/...")
list.files("/....")       #Give errors if path doesn't exist as typed

So if you're in /users/parent/subdir and you want to reference something in parent, you must type out the full path - write.table(foo, "parent/name.ext") will tell R to make a file: /users/parent/subdir/parent/name.ext.

Upvotes: 8

Stephan Kolassa
Stephan Kolassa

Reputation: 8267

Sure:

write.table(foo,file="../bar/baz.txt")

Or you can use absolute paths - nomenclature will depend on your operating system.

Upvotes: 3

Related Questions