Sven gave a great answer for moderately sized files. But if the reason that you are doing this is because reading the entire file does not fit into memory then you need to take a different approach.
It may be simplest to use an external tool like Perl or AWK to preprocess the file to only have the lines that you want, you can use pipe
to read from the output of another program so that you do not have to create an intermediate file.
Another approach would be to transfer the file to a database, then select just the rows that you want from the database.
You can also loop through the file. If you explicitly open the file, then you can read a few rows at a time, keep just the ones that you want, then read the next chunk starting where you left off. The options to read.csv
to skip lines and limit the number of lines to read would be helpful here.