This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Error with reading zipped file using the R function read.table.

I am trying to read a zipped file into R from my working directory (D:/mywork) using read.table function in R as shown below:

setwd("D:/mywork")       # setting directory to read data from it
Data = read.table(gzfile("mywork/abc.txt.gz"),sep="\t")

Note: I set my directory using setwd("D:/mywork) to read this file from it.

But it gave the following error for reading this file

When I tried to write the path of this file in read.table function

Data = read.table(gzfile("D:/mywork/abc.txt.gz"),sep="\t")

it worked. So is there any help to let the function reads this file directly from the working directory instead of writing the path every time for each dataset.

r

By the way, read.table can read gzip'd files straightaway, like Data<- read.table('abc.txt.gz'), no need of gzfile().

1 answer

If your working directory is D:/mywork, then specifying a file named mywork/abc.txt.gz is asking for D:/mywork/mywork/abc.txt.gz. That's how relative paths work.

BTW, this isn't really a bioinformatics question.

I did that, and It doesn't work

setwd("D:/mywork/abc.txt.gz")
setwd("D:/mywork")      
Data = read.table(gzfile("abc.txt.gz"),sep="\t") ## <<-  "mywork" is not there

You don't need to specify twice the mywork dir, that's what Devon was saying

I got it. Thank a lot Tris and Devon.

Log in to answer this question.