0

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

Chase
  • 67,710
  • 18
  • 144
  • 161
Adam
  • 9
  • 1
  • You are trying to do something really unusual: importing a word processor document into a statistical processing tool. The example code you refer to will only work for data in comma-separated value or other similar tabular format. Try to export your data into a .csv file and try the import again. It may also be of help to read the R manuals, e.g: http://cran.r-project.org/doc/manuals/R-data.pdf – Andrie Feb 14 '11 at 17:40
  • Adam, I recommend taking a look at the R manual for data import and export here: http://cran.r-project.org/doc/manuals/R-data.pdf. You may also want to review the R2wd package for interacting with Word documents. The problem above is that R is expecting some sort of flat file structure (csv in this case) and not whatever bloat that Word puts on top of that. Resave that document as a text file3and it should work just fine. – Chase Feb 14 '11 at 17:40
  • Relevant post: [read an MSWord file into R](http://stackoverflow.com/questions/11111207/read-an-msword-file-into-r) – zx8754 May 13 '14 at 09:13

4 Answers4

6

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.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110
2

The link you posted is about a file that looks like this:

1997,3.1,4
1998,7.2,19
1999,1.7,2
2000,1.1,13

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.

Sacha Epskamp
  • 46,463
  • 20
  • 113
  • 131
2

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.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
0

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.

Maiasaura
  • 32,226
  • 27
  • 104
  • 108
  • 2
    -1 There are read functions in R that supports import from many proprietary formats, including Excel, SPSS, SAS, etc. Saving a Word format file into plain text will not guarantee that R can import it using read.table - the data has to be in a format that read.table will understand. – Andrie Feb 14 '11 at 18:25