[R] by function ??

David Winsemius dwinsemius at comcast.net
Sat Dec 12 22:35:59 CET 2009


On Dec 12, 2009, at 3:38 PM, L.A. wrote:

>
>
> 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

?ave   # creates a vector of length = length of original data.frame

abs( ave(dtst$ratio, dtst$LEAID, FUN=median)-dtst$ratio)
[1] 0.0911905 0.0488095 0.0488095 0.1088095 0.2021538 0.0000000  
0.0600000

>
> 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
>
> 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
>>> --
-- 

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list