开发者

How to import files into R?

I'm a new user to R, sorry if my question is too b开发者_如何学JAVAasic. 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.


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") 


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.


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.


This works with R:

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

c <- read.table("clipboard")

then press Enter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜