使用 Windows 的 R 中的文件路径问题(“字符串中的十六进制数字”错误)
我在 Windows 上运行 R,并在桌面上有一个 csv 文件。我按如下方式加载它,
x<-read.csv("C:\Users\surfcat\Desktop\2006_dissimilarity.csv",header=TRUE)
但 R 给出以下错误消息
错误:以“C:\U”开头的字符串中没有使用十六进制数字“\U”
那么加载此文件的正确方法是什么。我使用的是Vista
I run R on Windows, and have a csv file on the Desktop. I load it as follows,
x<-read.csv("C:\Users\surfcat\Desktop\2006_dissimilarity.csv",header=TRUE)
but the R gives the following error message
Error: '\U' used without hex digits in character string starting "C:\U"
So what's the correct way to load this file. I am using Vista
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
将所有
\
替换为\\
。它试图转义下一个字符,在本例中为
U
,因此要插入\
,您需要插入转义的\
,即\\
replace all the
\
with\\
.it's trying to escape the next character in this case the
U
so to insert a\
you need to insert an escaped\
which is\\
请不要将此响应标记为正确,因为 smitec 已正确回答。我在 .First 库中添加了一个便利函数,该函数可以将 Windows 路径转换为 R 中可用的格式(Sacha Epskamp 描述的方法)。只需将路径复制到剪贴板 (ctrl + c),然后以
pathPrep()
形式运行该函数。无需争论。该路径会正确打印到控制台并写入剪贴板,以便轻松粘贴到脚本。希望这有帮助。Please do not mark this response as correct as smitec has already answered correctly. I'm including a convenience function I keep in my .First library that makes converting a windows path to the format that works in R (the methods described by Sacha Epskamp). Simply copy the path to your clipboard (ctrl + c) and then run the function as
pathPrep()
. No need for an argument. The path is printed to your console correctly and written to your clipboard for easy pasting to a script. Hope this is helpful.解决方案
试试这个:
x <- read.csv("C:/Users/surfcat/Desktop/2006_dissimilarity.csv", header=TRUE)
解释< /strong>
R 无法正确理解正常的 Windows 路径,因为
"\"
具有特殊含义 - 它用作转义字符以赋予以下字符特殊含义 (\n
对于换行符,\t
表示制表符,\r
表示回车符,...,看看这里)。因为 R 不知道序列
\U
它会抱怨。只需将"\"
替换为"/"
或使用额外的"\"
来转义"\"
> 从其特殊含义来看,一切顺利。替代
在 Windows 上,我认为在 R 中使用 Windows 特定路径改进工作流程的最佳方法是使用允许自定义热键的 AutoHotkey:
slaches ...
AutoHotkey 代码片段 (链接到主页)
Solution
Try this:
x <- read.csv("C:/Users/surfcat/Desktop/2006_dissimilarity.csv", header=TRUE)
Explanation
R is not able to understand normal windows paths correctly because the
"\"
has special meaning - it is used as escape character to give following characters special meaning (\n
for newline,\t
for tab,\r
for carriage return, ..., have a look here ).Because R does not know the sequence
\U
it complains. Just replace the"\"
with"/"
or use an additional"\"
to escape the"\"
from its special meaning and everything works smooth.Alternative
On windows, I think the best thing to do to improve your workflow with windows specific paths in R is to use e.g. AutoHotkey which allows for custom hotkeys:
slaches ...
AutoHotkey Code Snippet (link to homepage)
我的解决方案是定义一个 RStudio 代码片段如下:
此代码片段将反斜杠
\
转换为双反斜杠\\
。如果您希望将反斜杠转换为正斜杠/
,则可以使用以下版本。定义首选代码段后,通过输入 p-p-TAB-ENTER 粘贴剪贴板中的路径(即 pp,然后按 Tab 键,然后 Enter)并且路径将神奇地插入 R 友好的分隔符。
My Solution is to define an RStudio snippet as follows:
This snippet converts backslashes
\
into double backslashes\\
. The following version will work if you prefer to convert backslahes to forward slashes/
.Once your preferred snippet is defined, paste a path from the clipboard by typing p-p-TAB-ENTER (that is pp and then the tab key and then enter) and the path will be magically inserted with R friendly delimiters.
运行 Windows 机器时,将反斜杠 \ 替换为正斜杠 /
Replace back slashes \ with forward slashes / when running windows machine
我知道这确实很旧,但如果您无论如何都要复制和粘贴,您可以使用:
readClipboard() 为您转义反斜杠。只需记住确保“.csv”包含在您的副本中,也许可以这样:
如果您确实想最大程度地减少输入,您可以使用一些功能:
I know this is really old, but if you are copying and pasting anyway, you can just use:
readClipboard() escapes the back-slashes for you. Just remember to make sure the ".csv" is included in your copy, perhaps with this:
And if you really want to minimize your typing you can use some functions:
在 Windows 上用正斜杠替换反斜杠对我有用。
Replacing backslash with forward slash worked for me on Windows.
对于包含文本挖掘数据(语音、新闻通讯等)的 txt 文件,处理此问题的最佳方法是将“\”替换为“/”。
例子:
The best way to deal with this in case of txt file which contains data for text mining (speech, newsletter, etc.) is to replace "\" with "/".
Example:
我认为 R 正在读取字符串中的 '\' 作为转义字符。例如,\n 在字符串中创建新行,\t 在字符串中创建新制表符。
'\' 会起作用,因为 R 会将其识别为普通的反斜杠。
I think that R is reading the '\' in the string as an escape character. For example \n creates a new line within a string, \t creates a new tab within the string.
'\' will work because R will recognize this as a normal backslash.
readClipboard()
也可以直接工作。将路径复制到剪贴板然后
显示为
readClipboard()
works directly too. Copy the path into your clipboardThen
appears as
一个简单的方法是使用 python。
在 python 终端类型中
A simple way is to use python.
in python terminal type