将Word2007转换为R
我刚刚开始使用 R,我不知道如何将文件从任何其他程序导入到 R 中。我尝试了一个从 Word 到 R 的基本示例。我使用此网站作为如何执行此操作的假设示例http://www.mayin.org/ajayshah/KB/R/html /r1.html。这是我输入的内容:
A<-read.table("C:\Users\anr28\Desktop\x.docx", sep=",", col.names=c("year", "my1", "my2"))
我有一个Microsoft Word 中名为“x”的文档,根据我计算机上的属性菜单,该文档以 docx 结尾。我完全按照他们在示例中所做的操作,但它不起作用。这是打印出来的错误消息,但我不知道如何解释它们。
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
line 1 did not have 3 elements
In addition: Warning message:
In read.table("C:\\Users\\anr28\\Desktop\\x.docx", sep = ",", col.names = c("year", :
incomplete final line found by readTableHeader on 'C:\Users\anr28\Desktop\x.docx'
请帮助,我正在尝试自己学习这个,但无法将文件带入以实际学习该程序的关键,这非常令人沮丧,这正是我真正追求的。谢谢
I'm just starting using R and I can't figure out how to infile files from any other program into R. I tried a basic example from going to Word to R. I used this website as a supposed example on how to do this http://www.mayin.org/ajayshah/KB/R/html/r1.html. So here is what I typed:
A<-read.table("C:\Users\anr28\Desktop\x.docx", sep=",", col.names=c("year", "my1", "my2"))
I had a document named "x" in Microsoft Word which according to the properties menu on my computer ends with docx. I followed exactly what they did in the example and it didn't work. This was the error messages printed out, but I don't know how to interpret them.
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
line 1 did not have 3 elements
In addition: Warning message:
In read.table("C:\\Users\\anr28\\Desktop\\x.docx", sep = ",", col.names = c("year", :
incomplete final line found by readTableHeader on 'C:\Users\anr28\Desktop\x.docx'
Please Help, I'm trying to learn this on my own and it's very frustrating not being able to bring files in to actually learn the crux of the program, which is what I'm really after. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
read.table 函数(及相关函数)需要一个纯文本文件。 Word 使用自己的文件类型(因此是 .docx 扩展名),它不是纯文本,它包含您的数据(可能是压缩的)以及有关字体、颜色、大小和许多其他信息的信息,而 R 则没有理解。
最好的方法是在Word中打开文件,然后再次将其另存为纯文本文件(尝试单击左上角的圆圈,然后选择“另存为”,然后选择“其他格式”,然后在对话框中为“保存类型”选择“纯文本 (.txt)”选项)。然后按照示例将文本文件读入 R。
The read.table function (and related) expects a plain text file. Word uses its own file type (hence the .docx extension) which is not plain text, it includes your data (probably compressed) along with information about fonts, colors, sizes, and a bunch of other things in a way that R does not understand.
The best approach is to open your file in word, then save it again as a plain text file (try clicking the circle in the upper left corned, then choose "Save As", then choose "Other Formats", then in the dialog box choose the "Plain text (.txt)" option for "Save as type"). Then read the text file into R following the example.
您发布的链接是关于一个如下所示的文件:
“看起来像”意味着如果您在记事本等纯文本编辑器中阅读此文件,这就是您得到的内容。 Word 文件不是纯文本。纯文本文件是仅包含文本的文件(通常以 .txt 作为扩展名,但这不是必需的)。 Word文件是可以按单词打开和读取的文件,包含文本信息,还包含排版、字体等信息,以不可读的机器语言编码。用记事本打开word文档就可以看到区别。
正如其他答案中所述,您可以使用“另存为”将 Word 文件保存为纯文本文件。您还可以将 Excel 中的数据保存为纯文本文件,以便在 R 中轻松读取。
The link you posted is about a file that looks like this:
With "looks like" it is meant that if you read this file in a plain text editor like notepad, this is what you get. A word file is not plain text. A plain text file is a file (often with .txt as extension, but this is not necessary) that only contains text. A word file is a file that can be opened and read by word and contains information on the text, but also typesetting, fonts, etcetera, encoded in a machine language that is not readable. You can see the difference by opening the word document in notepad.
As said in other answers, you can save your word file as a plain text file with "save as". You can also save data from excel as a plain text file which can easily be read in R.
您可能想使用纯文本编辑器(而不是文字处理程序)来输入简单的数据文件 - 尝试使用记事本++,它与记事本一样易于使用,但具有更多功能。
Google 并下载它,然后输入一些逗号分隔的数字,保存并读入 R。
R for Windows 中还内置了一个基本文本编辑器,您可以使用它来键入 R 函数和数据文件。
You might want to use a plain text editor (not a word processor) for typing in simple data files - try notepad++, which is as easy to use as notepad but with a lot more functionality.
Google and download it, then enter some comma-separated numbers, save, and read into R.
There is a also a basic text editor built into R for Windows that you can use to type R functions and data files.
从专有的 Windows 格式将数据读入 R 是没有意义的。 R 会很乐意接受任何纯文本格式。对于您的情况,只需另存为纯文本并读入即可。
It makes no sense to read data into R from a proprietary windows format. R will happily accept any plain text format. In your case, just save as plain text and read it in.