I know fread
is relatively new, but it really gives great performance improvements. What I want to know is, can you select rows and columns from the file that you are reading? A bit like what read.csv.sql
does? I know using the select
option of the fread
one can select the columns to read, but how about reading only the rows which satisfy a certain criteria.
For example, can something like below be implemented using fread
?
read.csv.sql(file, sql = "select V2,V4,V7,V8,V9, V10 from file where V5=='CE' and V10 >= 500",header = FALSE, sep= '|', eol ="\n")
If this is not possible yet, is it advisable to read the entire lot of data, and then use subset
etc to arrive at the final result? Or will it defeat the purpose of using fread
?
For reference, I have to read about 800 files, each containing about 100,000 rows and 10 columns. Any input is welcome.
Thanks.
We're able to successfully import the CSV file using the fread() function. Note: We used double backslashes (\\) in the file path to avoid a common import error. Notice that we didn't have to specify the delimiter either since the fread() function automatically detected that it was a comma.
Its fread() function is meant to import data from regular delimited files directly into R, without any detours or nonsense. Note that “regular” in this case means that every row of your data needs to have the same number of columns.
table package comes with a function called fread which is a very efficient and speedy function for reading data from files. It is similar to read. table but faster and more convenient.
It is not possible to select rows with fread()
as with read.csv.sql()
yet. But it is still better to read the entire data (memory permitting) and then subset it as per your criteria. For a 200 mb file, fread()
+ subset()
gave ~ 4 times better performance than read.csv.sql()
.
So, using @Arun's suggestion,
ans = rbindlist(lapply(files, function(x) fread(x)[, fn := x]))
subset(ans, 'your criteria')
is better than the approach in the original question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With