Give scan()
a try. Using skip
you can skip already read lines and using nlines
you can specify the number of lines you would like to read. Then you can loop through the file.
> large <- 10000
> m <- matrix(sample(c(0,1),3*7,replace=TRUE), ncol=3)
> write.table(m, "test.txt")
> for(i in 0:large) {
+ l <- scan("test.txt", what = character(), skip = i, nlines = 1)
+ if(length(l) == 0) break
+ print (l)
+ }
Read 3 items
[1] "V1" "V2" "V3"
Read 4 items
[1] "1" "0" "1" "0"
Read 4 items
[1] "2" "0" "0" "0"
Read 4 items
[1] "3" "0" "0" "0"
Read 4 items
[1] "4" "0" "1" "1"
Read 4 items
[1] "5" "1" "1" "1"
Read 4 items
[1] "6" "1" "0" "1"
Read 4 items
[1] "7" "0" "0" "1"
Read 0 items
The code serves the purpose of illustrating how to apply scan()
and how to know when you have to stop reading.