Issue in opening a txt file in a specific directory in R, Ubuntu 14.04 LTS, R version 3.2.4 Revised

I got a very strange problem when trying to read via read.table a txt file in a folder. It recognizes the existence of the file (I debugged via print method) but it is not able to read the file into a table. Anybody has an idea of what is going wrong? I already looked at the other related topics but I didn't find any answer which fits my problem. Here is my code:

path1 = "/home/yoda/Desktop/thesis/TullyFisher/Galac.RC_Dwarfs/TFRCHI/bins_29_04/7bins_TF/datasets/TFR/"
out.file<-""
file.names1 <- dir(path1, pattern =".txt")
listofdfs<-list()
for(i in 1:length(file.names1))
{
     print(file.names1[i])
     file <- read.table(file.names1[i])
     df<-data.frame(as.numeric(file[[1]]),as.numeric(file[[2]]),as.numeric(file[[3]]),as.numeric(file[[4]]))
     listofdfs[[i]]<-df
     #write.table(listofdfs[[i]],file=paste0("outliers_",file.names1[i],quote=F,row.names = F, col.names = F))
}

It returns :

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

Upvotes: 0

Views: 32

Answers (2)

Akki
Akki

Reputation: 134

Error is because the files you are trying to read are not in current directory. R always try to read file from current directory.

To know your current directory try :

getwd()

it is different from the path1 for you.

So as menioned above by @R.S. please use full.names. Try below:

path1 = "/home/yoda/Desktop/thesis/TullyFisher/Galac.RC_Dwarfs/TFRCHI/bins_29_04/7bins_TF/datasets/TFR/"
out.file<-""
file.names1 <- dir(path1, pattern =".txt",full.names = T)

Upvotes: 0

R.S.
R.S.

Reputation: 2140

It must be the file path. That directory is not the working directory and dir() is only returning the filenames and not the full path . Using full.names argument should solve this. For example

dir("~/Desktop", full.names = T)

Upvotes: 2

Related Questions