Shravan Vasishth
2001-Dec-21 04:04 UTC
[R] read.table and as.vector (Was error message: "Error in model.frame")
Hi all, I figured out some things about an earlier question I posted today; this is a new question: I have a for-loop in which I have the following lines for (i in list.files(pattern = "*.dmp")) { currentfile <- read.table(i) vecrawrts <- as.vector(currentfile$V3) ... } Now, vecrawrts is numeric, but if I run the script on all the files with extension .dmp, I get the error message> for (i in list.files(pattern = "*.dmp")) {currentfile <- read.table(i) vecrawrts <- as.vector(currentfile$V3) residuals <- residuals(lm(v .... [TRUNCATED] Error in model.frame(formula, rownames, variables, varnames, extras, extranames, invalid variable type But if I do something like for (i in list.files(pattern = "*.dmp")) { currentfile <- read.table(i) n <- currentfile$V3 numn<-as.numeric(n) residuals <- residuals(lm(numn ~ vecwordlen)) ... } everything works fine. Why should this happen? Here is the full script containing the above loop: #----------------------------------------------------- foobar <- commandArgs()[3] # read in third command line argument # from shell script # (contains the wordlen file name) # read in word length as table; wordlen <- read.table(foobar) # convert it to a vector: vecwordlen <- as.vector(t(wordlen)) # for each file with extension .dmp for (i in list.files(pattern="*.dmp")){ # read in the file currentfile <- read.table(i) # convert 3rd column to a vector: n <- currentfile$V3 numn<-as.numeric(n) # compute residuals residuals <- residuals(lm(numn ~ vecwordlen)) # convert currentfile to matrix format matcurrentfile <- as.matrix(currentfile) # convert residuals to matrix format: matres <- as.matrix(residuals) # combine the original file with the residuals bound <- cbind(matcurrentfile,matres) # append to the output file: write(t(bound),file="rawresidualsoutput",ncolumns=4,append=TRUE) # clean up and start loop again rm(residuals,matres,currentfile,matcurrentfile,bound) } # after all files are done, remove the other objects rm(wordlen, vecwordlen) #--------------------------------------------------- Thanks, -- Shravan Vasishth Dept. of Linguistics, OSU 222 Oxley Hall, 1712 Neil Ave. Columbus, OH 43210-1298 USA URL: http://ling.ohio-state.edu/~vasishth ---------- Forwarded message ---------- Date: Thu, 20 Dec 2001 10:16:41 -0500 (EST) From: Shravan Vasishth <vasishth at julius.ling.ohio-state.edu> To: r-help at lists.R-project.org Subject: error message: "Error in model.frame" Hi all, All the files related to the question below are in http://ling.ohio-state.edu/~vasishth/TempRdir/ I've been using a script (called newresiduals.R, the code is in the above directory; I wanted to avoid cluttering up this message) that is run in the following manner at the command prompt: R --vanilla "wordlen-2-01-g1" < newresiduals.R What it does is: it goes through a bunch of files with extension .dmp, and (a) calculates residuals by doing residuals(lm(...)), using the vector version of wordlen-2-01-g1 as the independent variable, and the vector version of the third column of each .dmp file as the dependent variable, (b) adds a new column to the original .dmp file, containing the residuals. The above command is called from a shell script. This code has worked fine all these days on hundreds of files, but with the particular set I'm working with now, when I run the above command, I get the error message:> for (i in list.files(pattern = "*.dmp")) {currentfile <- read.table(i) vecrawrts <- as.vector(currentfile$V3) residuals <- residuals(lm(v .... [TRUNCATED] Error in model.frame(formula, rownames, variables, varnames, extras, extranames, :> traceback()9: model.frame.default(formula = vecrawrts ~ vecwordlen, drop.unused.levels = TRUE) 8: model.frame(formula = vecrawrts ~ vecwordlen, drop.unused.levels TRUE) 7: eval(expr, envir, enclos) 6: eval(mf, parent.frame()) 5: lm(vecrawrts ~ vecwordlen) 4: residuals(lm(vecrawrts ~ vecwordlen)) 3: eval.with.vis(expr, envir, enclos) 2: eval.with.vis(ei, envir) 1: source("newresiduals.R", echo = TRUE)> ?model.frame.defaultCan anyone help me figure out what's going wrong? The really odd thing is, the output of the script appears to be perfectly OK, even though it halts with this error message. Here's my system configuration, in case this is relevant:> version_ platform powerpc-unknown-linux-gnu arch powerpc os linux-gnu system powerpc, linux-gnu status major 1 minor 3.1 year 2001 month 08 day 31 language R Thanks very much in advance, -- Shravan Vasishth Dept. of Linguistics, OSU 222 Oxley Hall, 1712 Neil Ave. Columbus, OH 43210-1298 USA URL: http://ling.ohio-state.edu/~vasishth -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._