如何仅将满足条件的行从csv读取到R中? [英] How to read only lines that fulfil a condition from a csv into R?

查看:367
本文介绍了如何仅将满足条件的行从csv读取到R中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将大型csv文件读取到R中.我只想读取并使用满足特定条件的某些行(例如Variable2 >= 3).这是一个非常小的数据集.

I am trying to read a large csv file into R. I only want to read and work with some of the rows that fulfil a particular condition (e.g. Variable2 >= 3). This is a much smaller dataset.

我想直接将这些行读取到数据框中,而不是将整个数据集加载到数据框中,然后根据条件进行选择,因为整个数据集不容易装入内存中.

I want to read these lines directly into a dataframe, rather than load the whole dataset into a dataframe and then select according to the condition, since the whole dataset does not easily fit into memory.

推荐答案

您可以在sqldf包中使用read.csv.sql函数,并使用SQL select进行过滤.从read.csv.sql的帮助页面:

You could use the read.csv.sql function in the sqldf package and filter using SQL select. From the help page of read.csv.sql:

library(sqldf)
write.csv(iris, "iris.csv", quote = FALSE, row.names = FALSE)
iris2 <- read.csv.sql("iris.csv", 
    sql = "select * from file where `Sepal.Length` > 5", eol = "\n")

这篇关于如何仅将满足条件的行从csv读取到R中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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