[R] Odp: Aggregation and the meaning of class

Gabor Grothendieck ggrothendieck at gmail.com
Mon Jun 2 16:35:10 CEST 2008


To discover the available methods use the methods function, e.g.

example(by) # script creates a variable tmp of class by
class(tmp) # "by"
methods(class = "by") # describe.by print.by

Also try this:

> by # by is S3 generic since it uses UseMethod
function (data, INDICES, FUN, ...)
UseMethod("by")
<environment: namespace:base>
> methods(by) # by.* methods
[1] by.data.frame by.default

For a generic with a much longer list of methods try methods(print)

On Mon, Jun 2, 2008 at 10:14 AM, Chip Barnaby <cbarnaby at wrightsoft.com> wrote:
> Hi Petr,
> Thanks for your reply.  See below.
>
> At 05:58 AM 6/2/2008, Petr PIKAL wrote:
>>
>> Hi
>>
>> I did not see any response yet so I try a kick. Each object in R has some
>> attributes which can be revealed by
>>
>> attributes(object)
>>
>> Some of them are useful for methods and tell R how to handle particular
>> object with a method. There are other options how to evaluate your object
>> e.g. str, typeof, mode. If you went through intro manual you could find
>> that there are different types of objects like vectors, matrices,
>> data.frames, arrays, and lists. And probably some others.
>>
>> Output from by is list or array and you can handle such objects in many
>> ways.
>
> I understand all this in general.  I am a C++ programmer with decades of
> experience, so I fully understand object-oriented concepts.
>
> My question is: How does one learn the specifics?  For the case at hand,
> what are the specialized methods associated with class "by"? Are there
> clever things I can do with it?  Is there documentation for class "by"?
>  What are the browsing/exploring approaches one might use to learn about
> that class?
>
> So far, the main approach I have is interactive experimentation, yielding
> slow knowledge accretion.  Is there anything more efficient?  I have a
> moderate pile of R books, they are helpful, but obviously don't cover every
> detail of every situation.
>
>
>> BTW it is hard to tell what is desired result is as we do not have D60.
>
> Agreed and I could supply some example data if you want.
>
> I speculated that experienced users could answer my question on formal
> grounds, but perhaps this speaks to my general issue: questions like this
> aren't easily answered without running experiments.
>
> At the moment, my code "seems to be working."  I don't find that very
> reassuring.  I may have to get used to an "R culture" that is different from
> my prior world.
>
>
>> Eg. when I do
>>
>> > WM = by( D60, D60[ "KeyProfA"], FUN=function(x) weighted.mean( x$IAC,
>> x$Wt))
>> Error in by(D60, D60["KeyProfA"], FUN = function(x) weighted.mean(x$IAC, :
>>
>>  object "D60" not found
>>
>> I get an error.
>>
>> Better then using by is maybe to use ave which retains number of rows, but
>> I am not sure if it can be adopted to weighted mean.
>>
>> Regards
>>
>> Petr
>> petr.pikal at precheza.cz
>> 724008364, 581252140, 581252257
>>
>>
>> r-help-bounces at r-project.org napsal dne 30.05.2008 16:00:50:
>>
>> > Dear R-ers,
>> >
>> > My aggregation saga continues.
>> >
>> > Using the following sequence, I can calculate any statistic for row
>> > groups and merge the result back to all associated rows ...
>> >
>> >  > WM = by( D60, D60[ "KeyProfA"], FUN=function(x) weighted.mean( x$IAC,
>> x$Wt))
>> >
>> >  > D60$IAC.WM = as.numeric( WM[ D60$KeyProfA])
>> >
>> >  > class( WM)
>> > [1] "by"
>> >
>> > Questions ...
>> >
>> > 1) Is this a reasonable way to obtain the desired result?
>> >
>> > 2) What can one glean by knowing the class of WM ("by")?  It appears
>> > to me that class is a pretty shallow attribute in R ... just an
>> > associated string that selects among methods in some contexts.  Is
>> > that really all there is to it?  Is there a way to discover what
>> > generic methods are aware of a given class?  In other words, who
>> > cares if WM is a "by" ... what does that do for me?
>> >
>> > In my traditional universe (C++) I can grep and discover what methods
>> > are virtual, who inherits from whom, etc.  In R, the documentation
>> > appears silent on what is a "by" (correct me if I'm wrong).  In
>> > addition, I have found no way to broadly search code to learn
>> > things.  (Displaying single functions is useful but hardly broad.)
>> >
>> > How does one learn R more efficiently than randomly discovering how
>> > to avoid error messages?  (For example, I now know that a "by" cannot
>> > be coerced into a data.frame (although it seems to me that such a
>> > conversion could be usefully defined), so now I don't hit myself on
>> > the head with that particular hammer.)
>> >
>> > Chip Barnaby
>> >
>> >
>> >
>> >
>> >
>> >
>> > ---------------------------------------------------------
>> > Chip Barnaby                   cbarnaby at wrightsoft.com
>> > Vice President of Research
>> > Wrightsoft Corp.               781-862-8719 x118 voice
>> > 131 Hartwell Ave               781-861-2058 fax
>> > Lexington, MA 02421         www.wrightsoft.com
>> >
>> > ______________________________________________
>> > R-help at 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.
>
> ---------------------------------------------------------
> Chip Barnaby                   cbarnaby at wrightsoft.com
> Vice President of Research
> Wrightsoft Corp.               781-862-8719 x118 voice
> 131 Hartwell Ave               781-861-2058 fax
> Lexington, MA 02421         www.wrightsoft.com
>
> ______________________________________________
> R-help at 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.
>



More information about the R-help mailing list