Karl Ove Hufthammer wrote:
On Mon, 01 Mar 2010 06:37:30 -0500 Duncan Murdoch <murd...@stats.uwo.ca> wrote:
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 in columns.

Sure, I know that. But is there are reason why the '$' can't be overloaded to handle the extraction, as a *convenience* to the user?

See the second paragraph of my response.

Duncan Murdoch
After all, it *is* possible to extract columns by name from matrices
too (e.g., using d[,"Sepal.Width"]).

A similar type of overloading is used in the 'sp' class functions,
where you can basically treat a 'SpatialPointsDataFrame', a 'SpatialLinesDataFrame' or a 'SpatialPolygonsDataFrame' as a data frame, with '$colname' indexing and '[' subsetting, even though the *internals* of the objects have a completely different (and very complex) structure. But as a convenience to the user, you don't need to bother with the internals, and can handle the object *as if* it were a data frame. It's a very comfortable way of working.



______________________________________________
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