This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this questionI am trying to read a text file located in following paths. I am getting error if I read the file from long directory.. But if I place the file right under C, it runs fine. Could anyone tell me how to read table from following directory path ?
> data1 <-read.table("C:\\Documents and Settings Administrator\\My Documents\\My Dropbox\\data1.txt", sep="\t", header=TRUE)
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'C:\Documents and Settings Administrator\My Documents\My Dropbox\data.txt': No such file or directory
> data1 <-read.table("C:\\data1.txt",sep="\t",header=TR开发者_如何学PythonUE)
>
You are almost certainly missing a separator in
C:\\Documents and Settings Administrator\\My Documents\\My Dropbox\\data1.txt
It should read
C:\\Documents and Settings\\Administrator\\My Documents\\My Dropbox\\data1.txt
I think this, rather than the spaces, is the problem.
In R's string literals, the backslash character is used an escape character; this can be seen in your example, where "\t"
is resolved to a tab character. If you would like to use the blackslash itself, you should use a double backslash.
data1 <- read.table("C:\\Path\\To\\A\\File")
It's also OK to use a forward slash:
data1 <- read.table("C:/Path/To/A/File")
In addition, I would check the path carefully: in your pasted code it seems you might be missing a backslash between "Documents and Settings" and "Administrator".
In answer to your question title, there should be no problem with including spaces in a file path.
精彩评论