Read and write csv.gz file in R

谁都会走 提交于 2020-08-02 08:01:28

问题


There are very similar questions about this topic, but non deals with this under R quite precisely.

I have a csv.gz file and I would like to "unzip" the file and have it as ordinary *.csv file. I suppose one would go about first reading the csv.gz file and latter via write.csv command create the csv file itself.

Here what I have tried a part of other things:

gz.file <- read.csv(gzfile(file.choose()), as.is = TRUE)

gives:

  head(gz.file)
        farmNo.milk.energy.vet.cows
  1     1;862533;117894;21186;121
  2     2;605764;72049;43910;80
  3     3;865658;158466;54583;95
  4     4;662331;66783;45469;87
  5     5;1003444;101714;81625;125
  6     6;923512;252408;96807;135

File claims to be data.frame but doesn't behave like one, what I'm missing here?

 class(gz.file)
 [1] "data.frame"

Once read into memory I would like to have it in pure csv file, so would write.csv would be the solution?

write.csv(gz.file, file="PATH")

回答1:


In recent versions of data.table fast csv reader fread got support for csv.gz files. It automatically detects if it needs to decompress based on the filename so there is not much new to learn. Following should work.

library(data.table)
dt = fread("data.csv.gz")

This feature requires extra, fortunately lightweight, dependency as you can read in ?fread manual

Compressed files ending .gz and .bz2 are supported if the R.utils package is installed.

To write compressed argument use fwrite(compress="gzip").




回答2:


tidyverse has transparent support of gzip compressed files (and a few others)

library(tidyverse)

read_csv("file.csv.gz") -> d

# write uncompressed data
d %>% write_csv("file.csv")


来源:https://stackoverflow.com/questions/20609758/read-and-write-csv-gz-file-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!