简体   繁体   中英

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.

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 .

Sure:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM