[R-SIG-Finance] Elementary zoo question
Gabor Grothendieck
ggrothendieck at gmail.com
Sun Aug 6 21:35:51 CEST 2006
I think what you wanted to do was to subscript with a variable
of class "logical" variable but instead you subscripted with a
variable of class "zoo" :
class(x > 3) # "zoo"
Try:
x[which(x>3]]
On 8/6/06, Ajay Narottam Shah <ajayshah at mayin.org> wrote:
> I want to print out the cells in a zoo vector which satisfy a
> boolean. Here are a pair of lines that establishes the object `x':
>
> > library(zoo)
> > x <- structure(c(3.06998257357605, 3.33772140570104, 3.3633043045167,
> 3.41858136637240, 3.3658893681899, 3.70288557892392, 3.70802276353851,
> 3.47820398631861, 3.39745971102681, 3.27788314930746, 3.21152179993291,
> 3.19076418031979, 3.15816979006477, 3.22623617599131, 3.24522228272378,
> 3.24310489894978, 3.15909156737315, 3.18912820274379, 3.06356055461099,
> 3.01357540944708, 2.96377166092153, 2.91085513859483, 2.74785431983229,
> 2.58340878444823, 2.47441643715188, 2.27267478028934, 2.26052797003791,
> 1.97871265843542, 2.08038946495422, 2.07798508066967, 2.08428258775158,
> 2.13334354449931, 2.14911548113283, 2.14179508745853, 2.17909394235188,
> 2.07815119482578, 2.08190247495454, 2.09346304733713, 2.09247814243038,
> 1.88816169712064, 1.89600381030329), index = structure(c(13292,
> 13293, 13294, 13297, 13298, 13299, 13300, 13301, 13304, 13305,
> 13306, 13307, 13308, 13311, 13312, 13313, 13314, 13315, 13318,
> 13319, 13320, 13321, 13322, 13324, 13325, 13326, 13327, 13328,
> 13329, 13332, 13333, 13334, 13335, 13336, 13339, 13340, 13341,
> 13342, 13343, 13346, 13347), class = "Date"), class = "zoo")
>
> The obvious syntax, inspired by ordinary R vectors, doesn't work:
>
> > x[x>3]
> Data:
> numeric(0)
>
> Index:
> character(0)
>
> What gives? I'm reduced to:
>
> > for (i in 1:length(x)) {
> if (x[i] > 3) {
> print(x[i])
> }
> }
>
> Similarly, how would we print out rows from a zoo matrix based on a
> boolean condition that is applied one timepoint at a time?
>
> --
> Ajay Shah http://www.mayin.org/ajayshah
> ajayshah at mayin.org http://ajayshahblog.blogspot.com
> <*(:-? - wizard who doesn't know the answer.
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>
More information about the R-SIG-Finance
mailing list