[R-sig-Finance] How to get values from dataframe's column conditional on other column
Dirk Eddelbuettel
edd at debian.org
Tue Jun 13 13:52:26 CEST 2006
On Tue, Jun 13, 2006 at 12:35:36PM +0100, Yuri Volchik 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?
You would do yourself a huge favour if you read the 'An Introduction to R'
manual distributed with R, or, for that matter, any of the other contributed
introductory manuals on the R/CRAN site.
The answer, by the way, is
DF[ DF[,"Y"]==TRUE, "X" ]
ie you create a boolean condution for the rows (and == 'TRUE' could be
omitted if Y is boolean), and then select "X". There are still issues that
may confuse you about data.frames collapsing to a single vector, please go
and read the manual. Really.
Dirk
--
Hell, there are no rules here - we're trying to accomplish something.
-- Thomas A. Edison
More information about the R-SIG-Finance
mailing list