[R] Problem passing data into read.table()
dmcwilli
dmcwilli at utk.edu
Tue Apr 23 20:51:04 CEST 2002
Sorry for the delay, only just now got back to it ...
1. debug() did not help, would not show state of nrows inside read.table()
2. fill=T did help, could have dropped the last 2 rows (footer).
By accident I found the problem. read.table() returns a dataframe and the
result of the calculation of the number of rows has mode "list". Coercing it
to numeric solves the problem.
Working solution:
function(fname) {
# my.func(fname)
#
# Function to read file with header and footer
# Number of rows calculable from header row 8
#
grid.layout <- read.table(fname, as.is=T, header=F, sep="\t", comment.char="",
skip=7, nrows=1)
# coerce rows calculation to numeric
row.ctr <-
as.numeric(grid.layout[4]*grid.layout[5]*grid.layout[6]*grid.layout[7])
#read.table now sees row.ctr
tmp.df <- read.table(fname, as.is=T, header=T, sep="\t", comment.char="",
skip=20, nrows=row.ctr )
# drop empty first column
tmp.df <- tmp.df[, -1 ]
tmp.df
# end of my.func()
}
Thanks to all who helped.
regards,
>===== Original Message From Jason Turner <jasont at indigoindustrial.co.nz>
=====
>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
>close(con)
>
>#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
>changing.
>
>Cheers
>
>Jason
>--
>Indigo Industrial Controls Ltd.
>64-21-343-545
>jasont at indigoindustrial.co.nz
David McWilliams
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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