[R] two questions for R beginners
murdoch at stats.uwo.ca
Mon Mar 1 12:37:30 CET 2010
Karl Ove Hufthammer wrote:
> On Mon, 1 Mar 2010 11:02:59 +0100 Karl Ove Hufthammer <karl at huftis.org>
>>> * What were your biggest misconceptions or
>>> stumbling blocks to getting up and running
>>> with R?
>> Also I found it quite confusing that
> One more thing that still trips me up sometimes. '$' works on data
> frames but not on matrices (with dimnames/colnames). Even though the two
> objects *look* exactly the same, '$' on one of them works while '$' on
> the other gives a *very* confusing error message. Example:
> Some functions output matrices where you would expect them to output
> data frames, and then this problem occurs. (Is there a reason why '$'
> could/should not be made to 'work' on matrices too?)
The reason for the difference is that data.frames are lists organized
into columns (so the $ handling comes from the list, where it means
"extract the component") whereas a matrix is a single vector displayed
Of course, the problem is that a beginner only knows that they both look
the same. But I think the idea of a list is so fundamental to R that it
needs to be something learned pretty early, so I'd rather not blur the
distinction between dataframes and matrices.
More information about the R-help