[R] in continuation with the earlier R puzzle
Petr PIKAL
petr.pikal at precheza.cz
Tue Jul 13 13:41:03 CEST 2010
Hi
I do not use any of mentioned libraries so I can not directly answer it. I
would try to use debug(expr.frame) to see at what time the error is
thrown.
I have no idea why did you obtain error. Try to evaluate code in peaces
e.g. what is result of
list(MAf=quote(SMA(Last, 20)), MAs=quote(SMA(Last, 50)))
and look for differences between results got from spx data and nifty data.
Regards
Petr
Raghu <r.raghuraman at gmail.com> napsal dne 13.07.2010 13:17:42:
> Many many thanks to all of you. The beer cleared the air of doubts!
> Pls look at the following lines of code. This is taken from the example
of
> tradesys documentation. When I run the given example using the
data.frame spx
> it works just very fine but while I use some other data.frame (here
nifty) it
> crashes. Now I can intuit that the total rows in the column named "Last"
are
> 3637 and if i do a 20d MA and a 50d MA the respective rows for each of
them
> are 3618 and 3588. Why does expr.frame crash for one data.frame and not
for
> the other? I have given str() for both below for youe kind perusal.
>
> library(tradesys)
> > library(TTR)
> > x=nifty[,c("Open","Last")]
> > d <- expr.frame(x, list(MAf=quote(SMA(Last, 20)), MAs=quote(SMA(Last,
50))))
> Error in data.frame(c(1000, 1001.53, 987.17, 976.28, 960.32, 951.93,
949.29, :
> arguments imply differing number of rows: 3637, 3618, 3588
>
>
> str(nifty)
> 'data.frame': 3637 obs. of 6 variables:
> $ Date..GMT.: Factor w/ 3637 levels "01/01/1996","01/01/1997",..: 321
687 807
> 929 1052 1172 1537 1650 1764 1886 ...
> $ Open : num 1000 1002 987 976 960 ...
> $ High : num 1000 1002 987 976 960 ...
> $ Low : num 1000 989 977 963 952 ...
> $ Last : num 1000 989 978 964 953 ...
> $ Date : num 321 687 807 929 1052 ...
> > str(spx)
> 'data.frame': 14940 obs. of 5 variables:
> $ Open : num 16.7 16.9 16.9 17 17.1 ...
> $ High : num 16.7 16.9 16.9 17 17.1 ...
> $ Low : num 16.7 16.9 16.9 17 17.1 ...
> $ Close : num 16.7 16.9 16.9 17 17.1 ...
> $ Volume: num 1260000 1890000 2550000 2010000 2520000 2160000 2630000
> 2970000 3330000 1460000 ...
>
>
> Thanks
> Raghu
>
> On Tue, Jul 13, 2010 at 12:01 PM, Petr PIKAL <petr.pikal at precheza.cz>
wrote:
> Hi
>
> r-help-bounces at r-project.org napsal dne 12.07.2010 16:09:30:
>
> > When I just run a for loop it works. But if I am going to run a for
loop
> > every time for large vectors I might as well use C or any other
> language.
> > The reason R is powerful is becasue it can handle large vectors
without
> each
> > element being manipulated? Please let me know where I am wrong.
> >
> > for(i in 1:length(news1o)){
> > + if(news1o[i]>s2o[i])
> > + s[i]<-1
> > + else
> > + s[i]<--1
> > + }
> Think in R not in C. Why using loops when you can use whole object
> directly. It is like drinking beer from snifters. It is possible but
using
> pints is preferable and more convenient.
>
> news1o>s2o
>
> gives you a logical vector the same length
>
> and you can use it directly for further selection or computation. You
can
> consider FALSE as 0 and TRUE as 1 and use it as numeric vector
> so
>
> x<-runif(10)
> y<-runif(10)
>
> c(-1,1)[(x>y)+1]
>
> selects -1 when FALSE and 1 when TRUE.
>
> or you can use it in mathematical operation directly
>
> (x>y)*2-1
>
> Regards
> Petr
>
> >
> > --
> > 'Raghu'
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > 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.
>
>
>
> --
> 'Raghu'
More information about the R-help
mailing list