[R] Problem passing data into read.table()

Jason Turner jasont at indigoindustrial.co.nz
Tue Apr 23 23:24:31 CEST 2002

On Mon, Apr 22, 2002 at 03:21:34PM -0400, David R. McWillliams wrote:
> I thought about scan(), but I think that would require knowing the number
> of columns beforehand.  

Sounds like you need file(),readLines(), and strsplit(), with
a home-brewed data frame filler loop.

Something like (untested - just giving the idea here):

con <- file(fname)
header <- readLines(con, n=21)

#some magic here, to get the max number of columns
max.cols <- magic stuff

my.data <- readLines(con)

#and let's be hygenic

#chop off the last 2 lines, as they're footer stuff we don't want
#pushBack would probably work too, but I'm less familiar with it.
last.two <- ( length(my.data)-1 ):length(my.data)
my.data <- my.data[-last.two]

#split by tabs
my.data <- strsplit(my.data,"\t")

my.mat <- matrix(NA,ncol=max.cols,nrow=length(my.data))

for(i in seq(along=my.data)) {
	for(j in seq(along=my.data[[i]]) {
		my.mat[i][j] <- as.numeric(my.data[[i]][j])

> As I noted before, if I put in the number of
> rows in the second call to read.table(), say 'nrows=2000', it correctly
> detects the number of columns and rows.  I just can't get it to see the
> calculated value as in 'nrows=row.ctr'.  The intervening print() can fetch
> the value of row.ctr, so I don't understand why read.table() can't get it.

Not sure.  Have you played with debug() ?  I've found that very, very
handy for when I lose values - just walk slowly through the function,
one step at a time, and print out values as you think they might be 


Indigo Industrial Controls Ltd.
jasont at indigoindustrial.co.nz
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list