Message-Id: <m0xQF50-000hruC@franz.stat.wisc.edu> Date: Tue, 28 Oct 1997 11:05:22 -0600 (CST) From: Douglas Bates <bates@stat.wisc.edu> To: R-help@stat.math.ethz.ch Subject: R-beta: Assigning column names in a data frame You may recall that I was recently constructing a function to bootstrap the coefficients in a linear regression model. In S-PLUS I was using the model.matrix function applied to the fitted model, then taking the QR decomposition of that. I discovered that it was in fact easier to accomplish the bootstrapping in R because the QR decomposition of the model matrix is stored with the fitted model. So far, so good. Then I got fancy and returned the result as a data frame. "bsCoefSample" <- ## Construct a bootstrap sample of coefficients from a ## fitted regression model function(fittedModel, Nsampl) { value <- as.data.frame(t(coef(fittedModel) + qr.coef(fittedModel$qr, matrix(sample(resid(fittedModel), length(resid(fittedModel)) * Nsampl, repl = T), ncol = Nsampl)))) names(value) <- names(coef(fittedModel)) value } Later I thought I could cut this down a bit more by assigning the column names to the data frame as it was being created. It would look like "bsCoefSample" <- ## Construct a bootstrap sample of coefficients from a ## fitted regression model function(fittedModel, Nsampl) { as.data.frame(t(coef(fittedModel) + qr.coef(fittedModel$qr, matrix(sample(resid(fittedModel), length(resid(fittedModel)) * Nsampl, repl = T), ncol = Nsampl))), col.names = names(coef(fittedModel))) } but that doesn't work. Apparently the column names are being applied to the frame before the call to .Internal(data.frame(...)) in the data.frame function. At that point the frame argument has length 1 instead of the number of columns it will eventually have. A more simple example to illustrate this is R> ttt <- matrix(rnorm(10), ncol = 2) R> dim(as.data.frame(ttt)) [1] 5 2 R> as.data.frame(ttt, col.names = c("x", "y")) Error: names attribute must be the same length as the vector The R-like language marketed by MathSoft does not provide for a col.names argument in the as.data.frame function. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= 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@stat.math.ethz.ch =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=