Dataframes are lists. Look at dat with str and you will see that the third column (actually the third list element) is a matrix. It's not hard to find the documentation. If you read the documentation on the help page for data.frame you should see this:

"If a list or data frame or matrix is passed to data.frame it is as if each component or column had been passed as a separate argument (except for matrices of class"model.matrix" and those protected by I)."

It seems reasonable that poly() returns an object that is considered a model.matrix.

On Jul 17, 2009, at 12:54 PM, Ulrike Grömping wrote:


Dear UseRs,

I just learnt that the number of columns of a data frame is not always what I thought it to be, and I wonder where I should have learnt about this.
Consider the following example:

dat <- data.frame(X1=1:10, X2=LETTERS[1:10])
ncol(dat)          ## evaluates to 2 (of course)
dat$X1poly <- poly(dat$X1,3)
dat                  ## five columns displayed
ncol(dat)          ## evaluates to 3
colnames(dat)   ## three names (third is X1poly)
colnames(dat)[3] <- "newname"
dat                 ## all three previous X1poly columns renamed

This appears intentional, as it treats the column names reasonably. Where is
it documented ? Are there any other scenarios for which the number of
columns displayed when printing a data frame does not coincide with ncol ?

Regards, Ulrike

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to