[R] Logical operations or selecting data from data.frames

Roger Bivand Roger.Bivand at nhh.no
Tue Jan 9 19:05:25 CET 2007


On Tue, 9 Jan 2007, Benjamin Dickgiesser wrote:

> I suppose this doesn't work for the same reason as
> sdata$VaR < sdata$DdtdAbs && sdata$DdtdDuration >= 1
> 
> does only return  FALSE and not a vector of TRUE and FALSE as

See ?"&":

     '&' and '&&' indicate logical AND and '|' and '||' indicate
     logical OR.  The shorter form performs elementwise comparisons in
     much the same way as arithmetic operators.  The longer form
     evaluates left to right examining only the first element of each
     vector...

a <- rep(c(1,2), 5)
b <- rep(1,10)
a > 1 && b <= 1
a > 1 & b <= 1


> 
> sdata$VaR < sdata$DdtdAbs
> 
> would return.
> 
> Is there a ways around this?
> Benjamin
> 
> On 1/9/07, Benjamin Dickgiesser <dickgiesser at gmail.com> wrote:
> > Hi all,
> >
> > why doesn't something like this does not work?
> >
> > speedy <-
> >         (sdata$VaR < sdata$DdtdAbs) && sdata$DdtdDuration >= qpois(pct,lambda) &&
> >         sdata$Ddtd > MinDD
> >
> > or sdata$Ddtd[sdata$Ddtd > 0 && sdata$VaR < sdata$DdtdAbs]
> >
> > sdata looks like this:
> >
> >            dataId       date  value Ddtd VaR DdtdAbs DdtdDuration
> > 18948  79637 2004-07-27 10085.10           NA         NA    0.00            0
> > 18949  79638 2004-07-28 10117.10           NA         NA    0.00            0
> > 18950  79639 2004-07-29 10129.20           NA         NA    0.00            0
> > 18951  79640 2004-07-30 10139.70           NA         NA    0.00            0
> > 18952  79641 2004-08-02 10179.20           NA         NA    0.00            0
> > 18953  79642 2004-08-03 10120.20  0.579613329 336.060090   59.00            1
> > 18954  79643 2004-08-04 10126.50           NA         NA    0.00            0
> > 18955  79644 2004-08-05  9963.03  1.614279366 334.306978  163.47            1
> > 18956  79645 2004-08-06  9815.33  3.072828717 386.173057  311.17            2
> > 18957  79646 2004-08-09  9814.66  3.079445020 420.167049  311.84            3
> > 18958  79647 2004-08-10  9944.67           NA         NA    0.00            0
> > 18959  79648 2004-08-11  9938.32  0.063853300 328.315992    6.35            1
> > 18960  79649 2004-08-12  9814.59  1.308037371 379.182568  130.08            2
> >
> > I am trying to select rows from the data.frame which have Ddtd > x,
> > VaR < DdtdAbs and DdtdDuration > z.
> >
> > Thank you,
> > Benjamin
> >
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-help mailing list