[R] Reading data

Peter Ehlers ehlers at ucalgary.ca
Wed Oct 28 16:48:49 CET 2009


David Winsemius wrote:
> 
> On Oct 28, 2009, at 10:55 AM, Val wrote:
> 
>> The working directory is
>>
>>> getwd()
>> [1] "C:/Documents and Settings/Val/My Documents"
>>
>> The data file(Rossi.dat) and the script(Rossi.R) are in
>> "C:/Documents and Settings/Val/My Documents/R_data/prd"
> 
> So you are not giving a proper path when you issue the read.table 
> command. The default path when not explicitly provided is to the working 
> directory, and you have stored your data elsewhere.
> 
>>
>> How should I write to read the file?
>>
>>    source(???)   # what should be included here?
>>
>>
> 
> The guess I was about to make when I realized you were conflating data 
> and scripts was that you might want:
> 
> Rossi <- read.table(paste(getwd(), 'Rossi.dat', sep="/"), header=T) # 
> but that would not have been effectively different from the default 
> behavior.
> 
> So you instead want:
> 
> Rossi <- read.table(paste("C:/Documents and Settings/Val/My 
> Documents/R_data/prd", "Rossi.dat", sep="/"), header=T)

Sometimes it's easiest to use

  Rossi <- read.table(file.choose(), header=TRUE)

which allows the mouse-addicted to click away.

  -Peter Ehlers

> 
> Only if you wanted to read in a script with valid r-code would you use 
> source().
> 
> 
>>>
>>
>> I still  got the same error message.
>>
>> Error in file(file, "r") : cannot open the connection
>> In addition: Warning message:
>> In file(file, "r") : cannot open file 'Rossi': No such file or directory
>>
>>




More information about the R-help mailing list