Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to supply file names with paths to R's read.table function?

Tags:

r

What is the correct method for enter data(d=read.table("WHAT GOES HERE IF YOU HAVE A MACBOOK ") if you have a mac computer?

Also what does the error code list below mean:

d=read.table(“Firststatex.notepad”,header=T)
Error: unexpected input in "d=read.table(‚"
like image 774
Ashley Avatar asked Jan 10 '11 22:01

Ashley


1 Answers

Two usage errors:

  1. You don't use data() to read in to R datasets held in external files. data() is an R function to load datasets that are built in to R and R packages. read.table("foo.txt") will return a data frame object from the file "foo.txt", which you can assign to an object within R using the assignment operator <-, e.g.

    DF <- read.table("foo.txt")

    As for "what goes here...", you need to supply a file system path from the current directory to the directory holding the file you want to read in. If the file "foo.txt" is in the current working directory, you can just provide the file name with extension as I did above. If the file is in another directory you need to supply the path to the file name and the file name, for example if the file "foo.txt" is located in the directory above the current directory, you would supply "../foo.txt". If it were in a directory myData located in the directory above the current directory you could us "../myData/foo.txt". So paths can be relative to the current directory. You can also use the fully qualified path on your file system hierarchy.

    An alternative is to use the file.choose() function in place of the file name string. This will allow you to navigate to the file you wish to load interactively using a native file selection dialogue. This is what happens on Windows and I suspect also on Mac; not much different happens on Linux. For example:

    DF <- read.table(file.choose())

    You should probably look for specific help for your operating system if you are not familiar with how to specify file names and paths.

  2. I get the same error when copying and pasting in the code you provide. The problem comes from the fact that you are using fancy, curly quotes “Firststatex.notepad” rather than one of the three sets of accepted quote marks: ` , ", and '; each of these is acceptable, i) "Firststatex.notepad", ii) 'Firststatex.notepad', and iii) `Firststatex.notepad` Just because the quotes you used look like quotes to you or I, these aren't quotes as far as most computer programs recognise. MS Word often inserts these quotes when you enter " for example, as do many other applications.

like image 166
Gavin Simpson Avatar answered Nov 09 '22 09:11

Gavin Simpson