[R] why doesn't ifelse work ?

Andrew Robinson A.Robinson at ms.unimelb.edu.au
Fri Apr 29 04:24:43 CEST 2011


Hi Eric,

tough to say.  Please try to provide commented, minimal,
self-contained, reproducible code.

Cheers

Andrew

On Thu, Apr 28, 2011 at 06:46:16PM -0700, eric wrote:
> I have the following lines of code:
> 
> ind <- rollapply(GSPC, 200, mean)
> signal <- ifelse(diff(ind, 5) > 0 , 1 , -1)
> signal[is.na(signal)] <- 0
> 
> I never get a value of -1 for signal even though I know diff(ind , 5) is
> less than zero frequently. It looks like when diff(ind , 5) is less than
> zero, signal gets set to 0 instead of - 1. Any ideas why ?  Here's some
> information on ind and diff(ind, 5) :
> 
> > mode(diff(ind, 5) >0)
> [1] "logical"
> > class(diff(ind, 5) >0 )
> [1] "zoo"
> > str(diff(ind, 5) > 0 )
> ???zoo??? series from 1990-05-31 to 2010-12-02
>   Data: logi [1:5171, 1] FALSE FALSE FALSE FALSE FALSE FALSE ...
>  - attr(*, "dimnames")=List of 2
>   ..$ : NULL
>   ..$ : chr "GSPC.Adjusted"
>   Index:  Date[1:5171], format: "1990-05-31" "1990-06-01" "1990-06-04"
> "1990-06-05" "1990-06-06" "1990-06-07" "1990-06-08" "1990-06-11" ...
> > class(ind)
> [1] "zoo"
> > mode(ind)
> [1] "numeric"
> > str(ind)
> ???zoo??? series from 1990-05-23 to 2010-12-02
>   Data: num [1:5176, 1] 339 339 338 338 338 ...
>  - attr(*, "dimnames")=List of 2
>   ..$ : NULL
>   ..$ : chr "GSPC.Adjusted"
>   Index:  Date[1:5176], format: "1990-05-23" "1990-05-24" "1990-05-25"
> "1990-05-29" "1990-05-30" "1990-05-31" "1990-06-01" "1990-06-04" 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/why-doesn-t-ifelse-work-tp3482680p3482680.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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.

-- 
Andrew Robinson  
Program Manager, ACERA 
Department of Mathematics and Statistics            Tel: +61-3-8344-6410
University of Melbourne, VIC 3010 Australia               (prefer email)
http://www.ms.unimelb.edu.au/~andrewpr              Fax: +61-3-8344-4599
http://www.acera.unimelb.edu.au/

Forest Analytics with R (Springer, 2011) 
http://www.ms.unimelb.edu.au/FAwR/
Introduction to Scientific Programming and Simulation using R (CRC, 2009): 
http://www.ms.unimelb.edu.au/spuRs/



More information about the R-help mailing list