Error with reading zipped file using the R function read.table.
1
0
Entering edit mode
9.2 years ago
M K ▴ 660

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 • 6.7k views
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode
9.2 years ago

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.

ADD COMMENT
0
Entering edit mode

I did that, and It doesn't work

setwd("D:/mywork/abc.txt.gz")
ADD REPLY
1
Entering edit mode
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

ADD REPLY
0
Entering edit mode

I got it. Thank a lot Tris and Devon.

ADD REPLY

Login before adding your answer.

Traffic: 2047 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6