[R] Value Lookup from File without Slurping

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Fri Jan 16 11:43:12 CET 2009


if the file is really large, reading it twice may add considerable penalty:

r at quantide.com wrote:
> Something like this should work
>
> library(R.utils)
> out = numeric()
> qr = c("AAC", "ATT")
> n =countLines("test.txt")

# 1st pass

> file = file("test.txt", "r")
> for (i in 1:n){

# 2nd pass

> line = readLines(file, n = 1)
> A = strsplit (line, split = " ")[[1]][1]
> if(is.element(A, qr)) {
> value = as.numeric(strsplit (line, split = " ")[[1]][2])
> out = c(out, value)
> }
> }

if this is a one-go task, counting the lines does not pay, and why
bother.  if this is a repetitive task, a database-based solution will
probably be a better idea.

vQ




More information about the R-help mailing list