[R] by function ??

William Dunlap wdunlap at tibco.com
Sat Dec 12 22:18:57 CET 2009


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of L.A.
> Sent: Saturday, December 12, 2009 12:39 PM
> To: r-help at r-project.org
> Subject: Re: [R] by function ??
> 
> 
> 
> Thanks for all the help, They all worked, But I'm stuck again.
> I've tried searching, but I not sure how to word my search as 
> nothing came
> up.
> Here is my new hurdle, my data has 7 abservations and my 
> results have 2
> answers:
> 
> 
> Here is my data	
> 
>      LEAID     ratio  
> 3 6307     0.7200000
> 1 6307     0.7623810
> 2 6307     0.8600000
> 4 6307     0.9200000
> 5 8300     0.5678462
> 7 8300     0.7700000
> 6 8300     0.8300000
> 
> 
> > median<-summaryBy(ratio ~ LEAID, data = Dataset, FUN = median)
> 
> > print(median)
>   LEAID       ratio.median
> 1 6307        0.8111905
> 2 8300        0.7700000
> 
> Now what I want is a way to compute 
> abs(ratio- median)by LEAID for each observation to produce 
> something like
> this
> 
> LEAID     ratio          abs
> 3 6307     0.7200000     .0912
> 1 6307     0.7623810     .0488
> 2 6307     0.8600000     .0488 
> 4 6307     0.9200000     .1088 
> 5 8300     0.5678462     .2022 
> 7 8300     0.7700000     .0000 
> 6 8300     0.8300000     .0600

Try ave(), as in
   > Dataset$abs <- with(Dataset, ave(ratio, LEAID, FUN=function(x)abs(x-median(x))))
   > Dataset
     LEAID     ratio       abs
   3  6307 0.7200000 0.0911905
   1  6307 0.7623810 0.0488095
   2  6307 0.8600000 0.0488095
   4  6307 0.9200000 0.1088095
   5  8300 0.5678462 0.2021538
   7  8300 0.7700000 0.0000000
   6  8300 0.8300000 0.0600000

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

>  
> Thanks,
> L.A.
> 
> 
> 
> 
> Ista Zahn wrote:
> > 
> > Hi,
> > I think you want
> > 
> > by(TestData[ , "RATIO"], LEAID, median)
> > 
> > -Ista
> > 
> > On Tue, Dec 8, 2009 at 8:36 PM, L.A. <romsa at millect.com> wrote:
> >>
> >> I'm just learning and this is probably very simple, but I'm stuck.
> >>   I'm trying to understand the by().
> >> This works.
> >> by(TestData, LEAID, summary)
> >>
> >> But, This doesn't.
> >>
> >> by(TestData, LEAID, median(RATIO))
> >>
> >>
> >> ERROR: could not find function "FUN"
> >>
> >> HELP!
> >> Thanks,
> >> LA
> >> --
> >> View this message in context:
> >> http://n4.nabble.com/by-function-tp955789p955789.html
> >> Sent from the R help mailing list archive at Nabble.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.
> >>
> > 
> > 
> > 
> > -- 
> > Ista Zahn
> > Graduate student
> > University of Rochester
> > Department of Clinical and Social Psychology
> > http://yourpsyche.org
> > 
> > ______________________________________________
> > 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.
> > 
> > 
> 
> -- 
> View this message in context: 
> http://n4.nabble.com/by-function-tp955789p962666.html
> Sent from the R help mailing list archive at Nabble.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