Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Stat10010: Help importing data with R

Options
  • 01-11-2010 3:57pm
    #1
    Registered Users Posts: 71 ✭✭


    Ok, im having some trouble importing data with R (Lab 2). I have a txt file called pulse.txt which i have saved to my C drive in a folder called statistics and so the path name for the pulse.txt file should be "C:\Statistics\Pulse.txt".

    Sooooo, i type in: pulse<-read.table(“C:\statistics\pulse.txt”,header=TRUE)
    but i repeatedly get this error message:
    Error: unexpected input in "pulse <-read.table(“"

    If anyone can please give me some advice I'd be so greatfull because i really am on the brink of losing my mind after trying to get that one line right for the last 2 hours.... Thanks.


Comments

  • Registered Users Posts: 3,803 ✭✭✭El Siglo


    jripper wrote: »
    Ok, im having some trouble importing data with R (Lab 2). I have a txt file called pulse.txt which i have saved to my C drive in a folder called statistics and so the path name for the pulse.txt file should be "C:\Statistics\Pulse.txt".

    Sooooo, i type in: pulse<-read.table(“C:\statistics\pulse.txt”,header=TRUE)
    but i repeatedly get this error message:
    Error: unexpected input in "pulse <-read.table(“"

    If anyone can please give me some advice I'd be so greatfull because i really am on the brink of losing my mind after trying to get that one line right for the last 2 hours.... Thanks.

    Have you changed the directory of your files in R? Such that you set the directory to your "C:\Statistics" file? If you do it like that, you can import your file like:
    pulse<-read.table("pulse.txt", header=T)

    Also, have you checked your file for any spaces between characters? R tends to read these as separate columns. Make sure that you have no spaces between any characters in your excel sheet and then if you have to, save it as a tab delimited file and see if it works again.


  • Registered Users Posts: 71 ✭✭jripper


    Just got it, went to file and changed the working directory then typed in the command you suggested. Worked perfectly, now i can do my asgnmnt. Thanks for taking the time to reply, you're a hero. ;-)


  • Registered Users Posts: 13,010 ✭✭✭✭bnt


    Since R is orignally UNIX software, I expect it has problems handling backslashes in file paths. You could try replacing them with forward slashes ("C:/Statistics/Pulse.txt") or double backslashes ("C:\\Statistics\\Pulse.txt"), to see if that helps.

    Death has this much to be said for it:
    You don’t have to get out of bed for it.
    Wherever you happen to be
    They bring it to you—free.

    — Kingsley Amis



  • Moderators, Education Moderators, Motoring & Transport Moderators Posts: 7,395 Mod ✭✭✭✭**Timbuk2**


    Make sure you have the location exactly right (including capital letters), and also I would use double backslashes as such (this is also the directory I use).

    data<-read.table("H:\\FILES\\Statistics\\file.txt", header=TRUE)

    edit: bnt got there before me :o


  • Registered Users Posts: 3,803 ✭✭✭El Siglo


    No, it was a working directory problem (happens a lot when you start using the programme, takes time to get into the habit of setting the directory). When you don't set this to where you want to save etc... R automatically seeks out the "Documents Library" or "My Documents" as a default and that's no use to anyone.

    Op, you might want to invest in a copy of "Statistics: An Introduction Using R" by Michael Crawley. He's a website of tasks, commands etc... to work with while reading the book. It only costs about €25 but is a really handy book to have.


  • Advertisement
  • Closed Accounts Posts: 6,151 ✭✭✭Thomas_S_Hunterson


    try something like this:
    data<-read.table(file.choose(), header=TRUE)
    

    Avoids the hassle of escaping characters.


Advertisement