[R] why doesn't ifelse work ?

Dennis Murphy djmuser at gmail.com
Fri Apr 29 07:02:55 CEST 2011


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 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:6] "GSPC.Open" "GSPC.High" "GSPC.Low" "GSPC.Close" ...
  Index: Class 'Date'  num [1:5172] 7455 7456 7459 7460 7461 ...
> head(u)
           GSPC.Open GSPC.High GSPC.Low GSPC.Close GSPC.Volume GSPC.Adjusted
1990-05-31  -1.30300  -1.24530 -1.26890   -1.24725      295150      -1.24725
1990-06-01  -1.24750  -1.16410 -1.18735   -1.12970      313100      -1.12970
1990-06-04  -1.12995  -1.04920 -1.05475   -1.00325      334950      -1.00325
1990-06-05  -1.00345  -0.96625 -0.97210   -0.94540      134750      -0.94540
1990-06-06  -0.94560  -0.94210 -0.91795   -0.92290     -448450      -0.92290
1990-06-07  -0.92295  -0.91270 -0.89700   -0.90335     -544200      -0.90335

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.
>



More information about the R-help mailing list