n.readLines: Read 'n' lines (ignoring comments and header) from a file.

Description Usage Arguments Value Author(s) Examples

View source: R/reader.R

Description

Useful when you don't know the length/structure of a file and want a useful sample to look at. Can skip ahead in the file too. Copes well when there are less than 'n' lines in the file.

Usage

1
n.readLines(fn, n, comment = "#", skip = 0, header = TRUE)

Arguments

fn

name of the file(s) to get the length of

n

number of valid lines to attempt to read looks at the top few lines (ignoring comments)

comment

a comment symbol to ignore lines in files

skip

number of lines to skip at top of file before processing

header

whether to allow for, and skip, a header row

Value

returns the first n lines of the file meeting the criteria, or if 'skip' implies lines beyond the length of the file, the result,will be truncated - although in this case, the last line will always be read.

Author(s)

Nicholas Cooper nick.cooper@cimr.cam.ac.uk

Examples

1
2
3
4
5
6
7
orig.dir <- getwd(); setwd(tempdir()); # move to temporary dir
dat <- matrix(sample(100),nrow=10)
write.table(dat,"temp.txt",col.names=FALSE,row.names=FALSE)
n.readLines("temp.txt",n=2,skip=2,header=FALSE)
dat[3:4,]
unlink("temp.txt")
setwd(orig.dir) # reset working directory to original

Example output

Loading required package: NCmisc
[1] "35 21 43 71 73 47 83 54 76 19" "51 57 25 24 99 82 6 80 32 60" 
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]   35   21   43   71   73   47   83   54   76    19
[2,]   51   57   25   24   99   82    6   80   32    60

reader documentation built on May 2, 2019, 9:27 a.m.