在R中读写csv.gz文件 [英] Read and write csv.gz file in R

查看:510
本文介绍了在R中读写csv.gz文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于此的类似问题主题,但没有在R下很精确地处理这个问题.

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

我有一个csv.gz文件,我想解压缩"该文件并将其作为普通的* .csv文件.我想首先去读取csv.gz文件,然后再通过write.csv命令创建csv文件本身.

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)

给予:

  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

文件声称是data.frame但行为不像一个,这是我在这里所缺少的吗?

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

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

一旦读入内存,我想将其保存在纯csv文件中,那么write.csv将是解决方案吗?

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")

推荐答案

在最新版本的data.table快速csv阅读器fread中,它支持csv.gz文件.它会根据文件名自动检测是否需要解压缩,因此无需学习太多新知识.以下应该有效.

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")

此功能需要额外的幸运的轻量级依赖项,您可以在 ?fread手册

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

如果安装了R.utils软件包,则支持以.gz和.bz2结尾的压缩文件.

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

要编写压缩参数,请使用fwrite(compress="gzip").

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

这篇关于在R中读写csv.gz文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆