[R] Select rows from Data Frame with conditions

Rolf Turner rolf.turner at xtra.co.nz
Fri Mar 8 03:35:39 CET 2013


On 03/08/2013 02:22 PM, Sachinthaka Abeywardana wrote:
> Hi all,
>
> I have two dataframes. The first (A) contains all the stock prices for
> today including today. So the first column is the stock Symbol and the
> second column is the stock price. The second (B) is the symbol list in the
> top 100 stocks.
>
> I want to pick out from dataframe A only the rows containing the symbols
> from B. i.e. something like:
>
>              prices <- A[A[,1]==B,2]
>
> is there any way to do this without using a for loop, I have to do this 365
> times (i.e. for one year).

Is "B" a data frame, or a vector?

Any I think you want to do something like (untested):

     prices <- A[A[,1] %in% B[,1],2]

     cheers,

         Rolf Turner



More information about the R-help mailing list