[R] Range across a List

jim holtman jholtman at gmail.com
Thu Mar 27 00:48:55 CET 2008


This statement:

temp<-lapply(split(DF1,DF1$Trade.Date), function(.df) {
+
data.frame(DATE=.df$Trade.Date,RANGE=max(.df$New.Price)-min(.df$New.Pric
e))
+ })

has many results for DATE and one for RANGE; that is the reason you
are getting multiple copies.  Instead you need to write:

temp<-lapply(split(DF1,DF1$Trade.Date), function(.df) {
+
data.frame(DATE=.df$Trade.Date[1],RANGE=max(.df$New.Price)-min(.df$New.Pric
e))
+ })

where you only that the first ([1]) instance of Trade.Date

For the tapply, you can write:

nn=tapply(DF1$New.Price, DF1$Trade.Date, function(.x) max(.x) - min(.x))


On Wed, Mar 26, 2008 at 5:22 PM, Ravi S. Shankar <ravis at ambaresearch.com> wrote:
> I did the following
> DF<-do.call(rbind, pp2)
> DF1=na.omit(DF)
> DF1[,2]=as.Date(DF1[,2])
>
> str(DF)
> 'data.frame':   18660 obs. of  6 variables:
>
> I tried the following code
>
> temp<-lapply(split(DF1,DF1$Trade.Date), function(.df) {
> +
> data.frame(DATE=.df$Trade.Date,RANGE=max(.df$New.Price)-min(.df$New.Pric
> e))
> + })
>
> temp[[1]][1:5,]
>        DATE    RANGE
> 1 2006-12-29 1276.670
> 2 2006-12-29 1276.670
> 3 2006-12-29 1276.670
> 4 2006-12-29 1276.670
> 5 2006-12-29 1276.670
>
> What am I doing wrong?
>
> I also tried the below and that seemed to give me the range
>
> nn=tapply(DF1$New.Price, DF1$Trade.Date, range)
>
> > head(nn)
> $`2006-12-29`
> [1]    0.0074638 1276.6772880
>
> $`2006-12-31`
> [1]   4.673445 227.600000
>
> $`2007-01-31`
> [1]    0.0030772 1255.2080450
>
> $`2007-02-28`
> [1]    0.003978 1316.638200
>
> $`2007-03-29`
> [1]   5.25585 216.20000
>
> $`2007-03-30`
> [1]    0.0047214 1266.8250000
>
>
> Thanks
>
> Ravi Shankar S
>
>
>
> -----Original Message-----
> From: jim holtman [mailto:jholtman at gmail.com]
> Sent: Thursday, March 27, 2008 2:49 AM
> To: Ravi S. Shankar
>
> Cc: r-help at stat.math.ethz.ch; markleeds at verizon.net
> Subject: Re: [R] Range across a List
>
> I think something like this should work.  I will give you the range
> for each date across all the data:
>
> x <- do.call(rbind, pp2)
> tapply(x$New.Price, x$Trade.Date, range)
>
>
> On 3/26/08, Ravi S. Shankar <ravis at ambaresearch.com> wrote:
> > To add more clarity to my question
> >
> > My data pp2 is a list
> >
> >  (pp2[[1]])
> >      RIC Trade.Date Close.Price Currency.Code Convertion.Rate
> New.Price
> > ABCD.SZ   2008/02/29       15.30           CNY          0.1408
> > 2.154240
> > ABCD.SZ   2008/01/31       15.27           CNY          0.1392
> > 2.040048
> > ABCD.SZ   2007/11/30       11.07           CNY          0.1357
> > 1.502199
> > ABCD.SZ   2007/10/31       10.89           CNY          0.1340
> > 1.459260
> > ABCD.SZ   2007/09/28       12.77           CNY          0.1334
> > 1.703518
> >
> >  (pp2[[2]])
> > RIC      Trade.Date   Close.Price Currency.Code Convertion.Rate
> > New.Price
> > PQRS.SZ   2008/02/29        9.27           CNY         0.1408
> 1.305216
> > PQRS.SZ   2008/01/31        8.07           CNY         0.1392
> 1.123344
> > PQRS.SZ   2007/12/31        8.76           CNY         0.1371
> 1.200996
> > PQRS.SZ   2007/11/30        6.43           CNY         0.1357
> 0.872551
> > PQRS.SZ   2007/10/31        6.80           CNY         0.1340
> 0.911200
> > PQRS.SZ   2007/09/28        7.94           CNY         0.1334
> 1.059196
> >
> > And so on till (pp2[[1244]])
> >
> > Each of pp2[[i]] is a data frame. For each date I need to find the
> range
> > of New.Price across the list
> > i.e.for 2008/02/29   it would be
> > max(pp2[[i]]$New.Price[1])-min(pp2[[i]]$New.Price[1]) where i ranges
> > from 1 to 1244
> >
> > Thank you,
> > Ravi
> >
> >
> >
> >
> >
> > -----Original Message-----
> > From: markleeds at verizon.net [mailto:markleeds at verizon.net]
> > Sent: Thursday, March 27, 2008 2:12 AM
> > To: Ravi S. Shankar
> > Subject: Re: [R] Range across a List
> >
> > >From: "Ravi S. Shankar" <ravis at ambaresearch.com>
> > >Date: 2008/03/26 Wed PM 03:28:52 CDT
> > >To: r-help at stat.math.ethz.ch
> > >Subject: [R] Range across a List
> >
> > i think it's a dataframe ( it looks
> > like one )  or convert it to
> > one if it's not and then I think below should
> > work.
> >
> > temp<-lapply(split(pp2,pp2$Trade.Date), function(.df)
> > {
> >  data.frame(.df$Trade.Date[1],range(.df$New.Price))
> > })
> >
> > result<-do.call(rbind,temp)
> >
> > test it though because i didn't.
> >
> >
> > >Hi R,
> > >I have a list
> > >> class(pp2)
> > >[1] "list"
> > >
> > >> length(pp2)
> > >[1] 1244
> > >
> > >It is in the below format
> > >      RIC Trade.Date Close.Price Currency.Code Convertion.Rate
> > New.Price
> > >ABCD.SZ   2008/02/29       15.30        CNY          0.1408  2.154240
> > >ABCD.SZ   2008/01/31       15.27        CNY          0.1392  2.125584
> > >ABCD.SZ   2007/12/31       14.88        CNY          0.1371  2.040048
> > >ABCD.SZ   2007/11/30       11.07        CNY          0.1357  1.502199
> > >ABCD.SZ   2007/10/31       10.89        CNY          0.1340  1.459260
> > >ABCD.SZ   2007/09/28       12.77        CNY          0.1334  1.703518
> > >
> > >I want to find the range of pp2$New.Price for length(pp2) for each
> date
> > >Any help would be appreciated
> > >
> > >Thanks in advance
> > >Ravi
> > >
> > >
> > >This e-mail may contain confidential and/or privileged
> > i...{{dropped:10}}
> > >
> > >______________________________________________
> > >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.
> >
> > This e-mail may contain confidential and/or privileged information. If
> you are not the intended recipient (or have received this
> > e-mail in error) please notify the sender immediately and destroy this
> e-mail. Any unauthorized copying, disclosure or distribution of
> > the material in this e-mail is strictly forbidden.  Any views or
> opinions presented are solely those of the author and do not
> > necessarily represent those of Amba Holdings Inc., and/or its
> affiliates.  Important additional terms relating to this email can be
> obtained
> > at  http://www.ambaresearch.com/disclaimer
> >
> >
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem you are trying to solve?
> This e-mail may contain confidential and/or privileged...{{dropped:17}}



More information about the R-help mailing list