[R] two questions for R beginners

Karl Ove Hufthammer karl at huftis.org
Mon Mar 1 13:07:52 CET 2010


On Mon, 01 Mar 2010 06:37:30 -0500 Duncan Murdoch <murdoch at 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? 
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.

-- 
Karl Ove Hufthammer



More information about the R-help mailing list