[R-SIG-Finance] R-Help(Aggregate function): Multiple functions for different collumns
Joshua Ulrich
josh.m.ulrich at gmail.com
Wed Sep 2 18:49:02 CEST 2009
Jatin,
Check out the plyr package. And there's a quick example of what
you're trying to do on this blog post:
http://www.cerebralmastication.com/?p=339
HTH,
Josh
--
http://www.fosstrading.com
On Wed, Sep 2, 2009 at 11:39 AM, jatin patni<jatinpatni at gmail.com> wrote:
> I'm trying to reduce the resolution or remove duplicate time stamps for the
> same symbols in the following sample file, in a way such that the new price
> is a volume weighted average for that second(timestamp) and the new volume
> is the sum of all volumes within that second.
>
> I'm trying to use sapply and aggregate separately but having trouble with
> the function. I want that the function should do a volume weighted avg for
> the price column in the subgroup(where timestamps and symbols are same) but
> a sum of volume in that group. How do I achieve both goals simultaneously.
>
> I did a lot of research on the mailing archives and spent hours on reading
> the help/faq but couldn't figure it out.
>
> Here's my codes that is working:-
>
> tmp1.split <- sapply(split(tmp1, tmp1$symbol),
> function(x){weighted.mean(tmp1$price, tmp1$volume)})
> tmp1.ag <- aggregate(tmp1, by=list(tmp1$symbol, tmp1$time),
> function(x){weighted.mean(tmp5$price, tmp5$volume)})
>
> How do I go ahead now(I'm confused and new to R)
> How do I write functions that handle multiple columns differently.
>
>>tmp1 #my dataframe
> sno symbol series time price volume
> 1 1 BHARTIARTL EQ 18:15:05 600.00 1
> 2 2 BHARTIARTL EQ 18:15:05 600.00 99
> 3 3 GLENMARK EQ 18:15:05 238.10 5
> 4 4 HINDALCO EQ 18:15:05 43.75 100
> 5 5 BHARTIARTL EQ 18:15:05 600.00 1
> 6 6 BHEL EQ 18:15:05 1100.00 11
> 7 7 HINDALCO EQ 18:15:06 43.20 1
> 8 8 CHAMBLFERT EQ 18:15:06 46.00 10
> 9 9 CHAMBLFERT EQ 18:15:06 46.00 90
> 10 10 BAJAUTOFIN EQ 18:15:06 80.00 100
>
> Thanks
>
> --
> Jatin Patni
> Tel: 91 9833 2180 52
> jatinpatni at gmail.com
> www.jatinpatni.co.nr
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>
More information about the R-SIG-Finance
mailing list