[R-sig-Finance] How to get values from dataframe's column conditional on other column
Gabor Grothendieck
ggrothendieck at gmail.com
Tue Jun 13 14:17:24 CEST 2006
Aaarg. Here it is again. Hopefully I got it right this time. Three
possibilities are:
> # test data
> DF <- data.frame(X = 1:4, Y = c(TRUE, TRUE, FALSE, FALSE))
> with(DF, X[Y]) # 1
[1] 1 2
> DF$X[DF$Y] # 2
[1] 1 2
> DF[DF$Y, "X"] # 3
[1] 1 2
On 6/13/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> Sorry, that should be:
>
> with(DF, X[Y,])
>
> or
>
> DF[DF$Y, "X"]
>
> On 6/13/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> > If DF is your data.frame
> >
> > with(DF, X[Y])
> >
> > or
> >
> > DF$X[DF$Y]
> >
> >
> >
> > On 6/13/06, Yuri Volchik <volchik2000 at list.ru> wrote:
> > > Hi to all members of this list,
> > >
> > > I'm quite a novice to R and was wondering if there is a more elegant
> > > way to solve a following problem:
> > > Suppose we have a dataframe
> > > X Y
> > > .12 TRUE
> > > -.24 TRUE
> > > .. ...
> > > .34 FALSE
> > >
> > > i.e. two (or more columns) with data and we want to get vector of X
> > > values conditional on Y values (say only X's when Y=TRUE).
> > > Of course it is possible to do it by looping through the whole
> > > dataframe, i was wondering if there is a a more elegant solution to
> > > this in R?
> > >
> > > --
> > > Best regards,
> > > Yuri mailto:volchik2000 at list.ru
> > >
> > > _______________________________________________
> > > 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