Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Erniecranks

macrumors newbie
Original poster
Apr 10, 2011
11
0
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
 
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.

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
 
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.
 
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.

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
 
  • Like
Reactions: Vinnu
Thank you

thank you thank you thank you thank you thank you .

I'll be back when I get to the second line:)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.