-6

I'm reading in a text file which is just a column of company names. Most of the company names contain more than one word. When I run the below two lines I get the given undesired output. I thought \n should do the trick as each company name is on a new line in the text file. Any help would be great.

Names=read.table(path2, sep="\n")
Names

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            JOHNSON & JOHNSON
196                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     JOHNSON CONTROLS INC
197                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                KROGER CO
198                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     LOCKHEED MARTIN CORP
199 LOWES COMPANIES INC\nMARATHON OIL CORP\nMICROSOFT CORP\nMURPHY OIL CORP\nNORTHROP GRUMMAN CORP\nOCCIDENTAL PETROLEUM CORP\nPEPSICO INC\nPFIZER INC\nPROCTER & GAMBLE CO\nRITE AID CORP\nSAFEWAY INC\nSEARS HOLDINGS CORP\nSPRINT NEXTEL CORP\nSUNOCO LOGISTICS PARTNERS LP\nSUPERVALU INC\nSYSCO CORP\nTARGET CORP\nTECH DATA CORP\nTESORO CORP\nTIME WARNER INC\nTYSON FOODS INC  -CL A\nUNITED PARCEL SERVICE INC\nUNITED TECHNOLOGIES CORP\nUNITEDHEALTH GROUP INC\nVALERO ENERGY CORP\nVERIZON COMMUNICATIONS INC\nWAL-MART STORES INC\nWALGREEN CO\nWELLPOINT INC\n
DanRoDuq
  • 260
  • 3
  • 13
  • 2
    So that is the result, but what does the input text data look like? – thelatemail Jul 09 '13 at 01:31
  • 1
    @user2486956 I down voted because thelatemail asked for the input text data. You've responded elsewhere but not to his request. It's darn near impossible to figure this out without a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Tyler Rinker Jul 09 '13 at 02:12

1 Answers1

4

You should use readLines instead of read.table. (And it really makes no sense to use "\n" as a separator since it is already the end-of-line marker.)

 Names=readLines(path2)
 str(Names)
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • `readLines` is more appropriate, but I can't see why the `read.table` function used should fail - it works on a simple example using `read.table(text=textdata,sep="\n")` – thelatemail Jul 09 '13 at 01:35
  • Ah okay thanks. Now, I'm wondering why my question is getting voted down. I googled so many different things, but having no a priori knowledge of such a function it is reasonable that I did not know how to look for it, no? – DanRoDuq Jul 09 '13 at 01:35
  • 2
    @user2486956 - It got downvoted (not by me) because you provided no example input data. A google search of something quite simple like `R read text lines` would have given you at least 4 answers just in the top responses. – thelatemail Jul 09 '13 at 01:39
  • The usual reason to see really long strings of data in one vector element is an unmatched quote (single or double). If you wanted to test that idea, you could see what you get with `read.table(path2, quote="")` – IRTFM Jul 09 '13 at 01:44