Skip to content

Range across a List

4 messages · Ravi S. Shankar, jim holtman

#
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
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.
New.Price
i...{{dropped:10}}
http://www.R-project.org/posting-guide.html
This e-mail may contain confidential and/or privileged i...{{dropped:10}}
#
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:
#
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)
$`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:
New.Price
1.305216
1.123344
1.200996
0.872551
0.911200
1.059196
range
date
#
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: