[R] Using files as connections
maj@stats.waikato.ac.nz
maj at stats.waikato.ac.nz
Thu Aug 28 01:19:01 CEST 2003
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
More information about the R-help
mailing list