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

David R. McWillliams dmcwilli at utk.edu
Mon Apr 22 19:12:57 CEST 2002


I am trying to read in a tab-delimited data file with a 21 row header and
2 row footer using two calls to read.table().  Numbers of rows and columns
are variable.  The header contains information for calculating the number
of rows of data.  I can successfully pick this out and calculate the
number of rows to read, but cannot get the second read.table() to assign
this number to "nrows"  (the number is correct; if I enter it manually,
the everything works fine).  Currently the function reads all the way to
the end and crashes on the footer, since the number of fields is different
from that of the data.

I know this could easily be done with some Perl pre-processing of the
file, but it is going to run on a Windows machine and I am trying to
minimize the number of packages to download.  Nevertheless, there is the
general problem of why I cannnot pass a calculated value into the 
function.

Code follows.

#
# function to read data with header and footer
#

# pick line 8 with the data layout information and calculate the number of rows ...
grid.layout <- read.table(fname, as.is=T, header=F, sep="\t", comment.char="", skip=7, nrows=1)
row.ctr <- grid.layout[4]*grid.layout[5]*grid.layout[6]*grid.layout[7]

# tells me I have the right dimensions ...
print(row.ctr)

# but they do not get passed into this read.table() ...
tmp.df <- read.table(fname, as.is=T, header=T, sep="\t", comment.char="", skip=20, nrows=row.ctr )
tmp.df

# end function


Regards,



David R. McWilliams
dmcwilli at utk.edu




-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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