Sachinthaka Abeywardana
2013-Mar-08 01:22 UTC
[R] Select rows from Data Frame with conditions
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). Thanks, Sachin [[alternative HTML version deleted]]
Something along the lines of top100 <- A[match(B,A[,1]),] Please provide R code with sample data and desired output. See http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Sachinthaka Abeywardana <sachin.abeywardana at gmail.com> 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). > >Thanks, >Sachin > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org 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.
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