user1209
user1209

Reputation:

How to import files into R?

I'm a new user to R, sorry if my question is too basic. I've installed the newest version of R on windows 7 and as a practice, i tried to open a .txt file using

students<-read.table("students.txt",header=T,sep="\t")

but there's always an error message as follows:

Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
  cannot open file 'students.txt': No such file or directory

I've already changed the directory to where the students.txt resides; but it just doesn't work. Please help!

Thank you very much in advance if anyone can help me solve the problem.

Upvotes: 9

Views: 95426

Answers (4)

karl
karl

Reputation: 21

This works with R:

first copy the table; then open up R and type:

c <- read.table("clipboard")

then press Enter.

Upvotes: 2

Alex Brown
Alex Brown

Reputation: 42872

If you use RStudio, which I thoroughly recommend, then you can create a Project with it's own directory. read.csv and friends will then read from that directory by default, which makes it all nice and easy.

Upvotes: 6

Dirk is no longer here
Dirk is no longer here

Reputation: 368201

There is an entire R manual devoted to data import / export questions.

Now, if you fail to specify file name and path correctly, use the file.choose() function instead which allows you to point, click and shoot:

students<-read.table(file.choose(),header=T,sep="\t") 

Upvotes: 24

Shane
Shane

Reputation: 100164

The error message is saying that it cannot find the students.txt file. You should try two things:

  1. Call getwd() and confirm that the file is in the directory that is returned.
  2. Try specifying the full path to the file in your read.table() call.

Look at help("read.table") for more detail on the function. You may, for instance, want to change the default to stringsAsFactors=FALSE, depending on your application.

Upvotes: 9

Related Questions