[R] two questions for R beginners

Duncan Murdoch murdoch at stats.uwo.ca
Mon Mar 1 15:09:11 CET 2010

Karl Ove Hufthammer wrote:
> 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? 

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.

More information about the R-help mailing list