Re: [R] why doesn't ifelse work ?

From: Dennis Murphy <djmuser_at_gmail.com>
Date: Thu, 28 Apr 2011 22:02:55 -0700

Hi:

On Thu, Apr 28, 2011 at 7:28 PM, eric <ericstrom_at_aol.com> wrote:
> equire(quantmod)
> require(PerformanceAnalytics)
> rm(list=ls())

# Could you please not do this in the middle of a code chunk?
# Anyone who copies/pastes this into his/her session will lose everything
# (s)he had been doing.

> getSymbols("^GSPC", src="yahoo", from="1990-01-01", to=Sys.Date())
> GSPC <-na.omit(Ad(GSPC))
> ind <- rollapply(GSPC, 200, mean)
> signal <- ifelse(diff(ind, 5) > 0 , 1 , -1)
> signal[is.na(signal)] <- 0

ind returns what is effectively a matrix with six columns. If you do the same with the differences,

> u <- diff(ind, 5)
> str(u)
‘zoo’ series from 1990-05-31 to 2010-12-03   Data: num [1:5172, 1:6] -1.303 -1.248 -1.13 -1.003 -0.946 ...

This informs you why the ifelse() statement doesn't make sense. Perhaps this is what you had in mind:

> apply(u, 2, function(x) table(sign(x)))
$GSPC.Open

  -1 1
1285 3887

$GSPC.High

  -1 1
1283 3889

$GSPC.Low

  -1 0 1
1284 1 3887

$GSPC.Close

  -1 1
1286 3886

$GSPC.Volume

  -1 0 1
1349 1 3822

$GSPC.Adjusted

  -1 1
1286 3886

## with

> signal <- sign(u)
> head(signal)

           GSPC.Open GSPC.High GSPC.Low GSPC.Close GSPC.Volume GSPC.Adjusted
1990-05-31        -1        -1       -1         -1           1            -1
1990-06-01        -1        -1       -1         -1           1            -1
1990-06-04        -1        -1       -1         -1           1            -1
1990-06-05        -1        -1       -1         -1           1            -1
1990-06-06        -1        -1       -1         -1          -1            -1
1990-06-07        -1        -1       -1         -1          -1            -1

> sum(is.na(signal))

[1] 0
> sum(signal == 0L)

[1] 2

Thank you for providing a reproducible example.

HTH,
Dennis

>

> --
> View this message in context: http://r.789695.n4.nabble.com/why-doesn-t-ifelse-work-tp3482680p3482737.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.
>

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. Received on Fri 29 Apr 2011 - 05:06:55 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 29 Apr 2011 - 05:20:34 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive