[R] two questions for R beginners

(Ted Harding) Ted.Harding at manchester.ac.uk
Mon Mar 1 13:25:20 CET 2010

On 01-Mar-10 12:07:52, 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? 
> 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

I'm not sure that "SpatialPointsDataFrame" is a dataframe (despite
the name)! Is it not simply a list? In which case, using "$" is
what you have to do to get at its components.


E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 01-Mar-10                                       Time: 12:25:17
------------------------------ XFMail ------------------------------

More information about the R-help mailing list