Importing CSVs in R statistical package

Discussion in 'Mac Apps and Mac App Store' started by Erniecranks, Apr 10, 2011.

  1. Erniecranks macrumors newbie

    Joined:
    Apr 10, 2011
    #1
    Help! I am a newbie to R, and I'm moving very slowly. I created a simple table which I named "experiment.csv" and I saved it to my desktop. It has 4 categories that use the alphabet, and numerical values under those categories.
    I attempted to read that file into R using:
    experiment <-read.table(users/myname/desktop/experiment.csv, header = TRUE), and I keep getting errors, saying
    " Error in read.table(file = file, header = header, sep = sep, quote = quote, :
    object 'Users' not found"

    I have also tried this with a slash before users, and I get an unexpected character error.

    Any help greatly appreciated.

    Ernie
     
  2. chaostik macrumors newbie

    Joined:
    May 29, 2008
    #2
    The whole filename needs to be in quotation marks and you will need the leading forwardslash. So it should read:

    Code:
    experiment <- read.table("/users/myname/desktop/experiment.csv", header = TRUE)
    Your subject and filename imply you're trying to import a CSV (ie comma separated), but you should note that the read.table function uses whitespace as the separator by default. You might actually want to use the function read.csv

    Just type ?read.csv at the R prompt to get the help file up.

    Hope that helps,

    Louis
     
  3. kuwisdelu macrumors 65816

    Joined:
    Jan 13, 2008
    #3
    Check three things.

    First, as stated above, the file name needs to be in quotes.

    Second, as stated above, make sure you're telling R the right delimiters. The function read.table() expects white space as separators, but you can specify what you used with sep="" but if it's actually a CSV where you used commas, you can also just use read.csv() instead.

    Third, if you don't want to have to bother with the whole file path or if you're running into problems where you're entering it wrong, you can drag-and-drop files and folders to R's console or source files and OS X will copy the file path into R for you.

    You can do read.csv("[drag-and-drop your csv file here]"). Alternatively, you can set your working directory by setwd("[drag-and-drop your project folder here]") and then you won't have to use the full file paths.
     
  4. chaostik macrumors newbie

    Joined:
    May 29, 2008
    #4
    Nice extra points kuwisdelu and they remind me of one more option which I rarely use myself, but know others who use it a lot:

    Code:
    experiment <- read.csv(file.choose(), header=TRUE)
    This causes a file dialog chooser to pop up and you just select the CSV that way.

    Louis
     
  5. Erniecranks thread starter macrumors newbie

    Joined:
    Apr 10, 2011
    #5
    Thank you

    thank you thank you thank you thank you thank you .

    I'll be back when I get to the second line:)
     

Share This Page