lineByLine(infile, outfile, linefunc = identity, choose.lines = NULL,
choose.columns = NULL, col.sep = " ", ask = TRUE,
blank.lines.skip = TRUE, verbose = TRUE, ...)
lineByLine
modifies each line using linefunc
. Default is the identity function. The user may define his or her own line-modifying functions, see Details for a thorough description.infile
. Positive values refer to lines to be chosen, whereas negative values refer to lines to be skipped. The vector cannot include both positive and negative values at the same time. If "NULL" (default), all lines are selected.infile
. The vector cannot include both positive and negative values at the same time. By default, all columns are selected without reordering among the columns. Duplication and reordering among the selected columns will occur in the modified file corresponding to the order in which the columns are listed.infile
. By default, col.sep = " "
(space). To split at all types of spaces or blank characters, set col.sep = "[[:space:]]"
or col.sep = "[[:blank:]]"
.lineByLine
ignores blank lines in the input.linefunc
. If choose.columns
contains invalid column numbers, this will also be displayed.linefunc
.read.table
can use a large amount of memory and be extremely time consuming.
Instead of reading the entire file at once, lineByLine
reads one line at a time, modifies the line using linefunc
, and then writes the line to outfile
.
The user may specify his or her own line-converting function. This function must take the argument x
, a character vector representing a single line of the file, split at spaces. However, additional arguments may be included.
If verbose
equals "TRUE", output should be displayed.
The modified vector is returned.
The framework of the line-modifying function may look something like this:lineModify <- function(x){ .xnew <- x
## Define any modifications, for instance recoding missing values in a dataset from NA to 0: .xnew[is.na(.xnew)] <- 0
## Just to monitor progress, display, for instance, 10 first elements, without newline: cat(paste(.xnew[1:min(10, length(.xnew))], collapse = " "))
## Return converted vector return(.xnew) }
See Haplin:::lineConvert
for an additional example of a line-modifying function.
convertPed
## Not run:
#
# ## Extract the first ten columns from "myfile.txt",
# ## without reordering
# lineByLine(infile = "myfile.txt", outfile = "myfile_modified.txt",
# choose.columns = c(1:10))
#
# ## End(Not run)
Run the code above in your browser using DataLab