使用data.table的fread读取具有选定行的csv文件 [英] Read csv file with selected rows using data.table's fread

查看:130
本文介绍了使用data.table的fread读取具有选定行的csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在经历一些较早的帖子-

I was going through some earlier post-

最快的读取方法之一CSV行

一种选择数据子集的方法是

One way to select subset of data is

write.csv(iris,"iris.csv")

fread("shuf -n 5 iris.csv")

但是我想知道是否可以通过一些 SQL 查询而不是前5行,例如仅导入那些具有 V6 = versicolor

However I was wondering if I can pass some SQL query instead of top 5 rows e.g. only import those rows that have V6 = versicolor

可以使用 fread 函数来做到这一点吗?

Is there any way to do this using fread function?

推荐答案

这在Windows中对我有用(unix替代为 grep )

This worked for me in windows (unix alternative is grep)

write.csv(iris,"iris.csv")

fread(cmd = paste('findstr', 'versicolor', 'iris.csv'))

    V1  V2  V3  V4  V5         V6
 1:  51 7.0 3.2 4.7 1.4 versicolor
 2:  52 6.4 3.2 4.5 1.5 versicolor
 3:  53 6.9 3.1 4.9 1.5 versicolor
 4:  54 5.5 2.3 4.0 1.3 versicolor
 5:  55 6.5 2.8 4.6 1.5 versicolor
 6:  56 5.7 2.8 4.5 1.3 versicolor
 7:  57 6.3 3.3 4.7 1.6 versicolor
 8:  58 4.9 2.4 3.3 1.0 versicolor
 9:  59 6.6 2.9 4.6 1.3 versicolor
10:  60 5.2 2.7 3.9 1.4 versicolor
11:  61 5.0 2.0 3.5 1.0 versicolor

它仅输出在任何字段中包含"versicolor"的那些行.

It outputs only those rows that contain "versicolor" in any field.

这篇关于使用data.table的fread读取具有选定行的csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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