I have been trying to read a random sample of lines from a file into a
data frame using readLines(). The help indicates that readLines() will
start from the current line if the connection is open, but presented with
a closed connection it will open it, start from the beginning, and close
it when finished.

In the code that follows I tried to open the file before reading but
apparently without success, because the result was repeated copies of the
first line:

flines <- 107165
slines <- 100
selected <- sort(sample(flines,slines))
strvec <- rep(“”,slines)
file(“c:/data/perry/data.csv”,open="r")
isel <- 0
for (iline in 1:slines) {
  isel <- isel + 1
  cline <- readLines(“c:/data/perry/data.csv”,n=1)
  if (iline == selected[isel]) strvec[isel] <- cline else
    isel <- isel - 1
}
close(“c:/data/perry/data.csv”)
sel.flows <- read.table(textConnection(strvec), header=FALSE, sep=",")


There was also an error "no applicable method"  for close.

Comments gratefully received.

Murray Jorgensen

______________________________________________
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help

Reply via email to