Reputation: 1
I need help to put a .txt
file into R. I'm trying to input a text document into R for a stats class but it isn't working.
I put it in read.table("TransaniaIncomes.txt", header=TRUE)
but the following message keeps coming up:
Error in file(file, "rt") : cannot open the connection In addition: Warning message: In file(file, "rt") : cannot open file 'TransaniaIncomes.txt': No such file or directory
I don't really know what to do. The file extends for several thousand characters like this in 5 straight columns:
3.755556 13.51542 14.4545 19.93651 21.62806
57.48426 7.282404 38.48386 10.22754 9.9399
21.38976 6.675126 32.14614 53.96588 12.37087
30.35346 10.20072 2.650464 26.84042 14.13381
Upvotes: 0
Views: 3292
Reputation: 3843
I. getwd()
Make sure your .txt
file is in the current working directory of RStudio. To check current working directory of RStudio, type in console: getwd()
and see if your file exists in that path.
II. setwd()
If your file exists in some other path, you can set that path in RStudio using setwd()
and then run the read.table()
command. Example:
setwd("C:/My/Path/To/TextFile") # The .txt file would be in TextFile folder
read.table("TransaniaIncomes.txt", header=TRUE)
III. file.choose()
To avoid path setting and you know where your file is stored, you can use file.choose()
function inside read.table()
which will open up a interactive dialog box to go and select your .txt
file from PC, wherever it is.
read.table(file.choose(), sep="\t", header=TRUE) # Choose "TransaniaIncomes.txt"
Note: file.choose()
would open a dialog box to choose file from you PC from any path.
If your txt
file is tab separated, you can include sep = "\t"
argument to read.table
.
header=TRUE
would come if you have column names in your text file, if not then it should be set to FALSE
.
Upvotes: 2
Reputation: 21
R told you that he cannont find your file. you have 2 options:
1) type getwd() in the terminal. It will show your path to the working directory. So put your file in that folder and it should work.
2) find the path of your file ex C:/doc/TransaniaIncomes.txt add the path to your read.table() function.
read.table("C:/doc/TransaniaIncomes.txt", header=TRUE)
tips: Make sure your file name is written correctly.
Upvotes: 0