[R] Odp: Aggregation and the meaning of class

Petr PIKAL petr.pikal at precheza.cz
Mon Jun 2 11:58:02 CEST 2008


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.

BTW it is hard to tell what is desired result is as we do not have D60.

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.



More information about the R-help mailing list