Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have imported a data from an excel sheet.

I want to extract parts of the list based on some conditions. First I want to create one list whit the The Coca-Cola data and then I want another list with McDonald data. The key thing is that I want the extraction process to be able to spot when the data moves from one ticker to another.

AA = MatrixForm[DUBS]
{{"Symbol", "Full Name", "Date", "Open", "High", "Low", "Close",   
   "Adj Close", "Volume", "Type"},
   {"KO", "The Coca-Cola Company", {2013, 7, 19, 0, 0, 0.}, 40.88, 41.1,
     40.79, 41.09, 41.09, 1.14011*10^7, "Stock"},{
   {"MCD", "McDonald", {2013, 7, 19, 0, 0, 0.}, 100.2, 100.41, 99.53, 
     100.27, 100.27, 4.5083*10^6, "Stock"},
   {"MCD", "McDonald", {2013, 7, 18, 0, 0, 0.}, 100.48, 100.77, 99.99, 
     100.18, 100.18, 3.4016*10^6, "Stock"},
   {"MCD", "McDonald", {2013, 7, 17, 0, 0, 0.}, 100.05, 100.35, 99.3, 
     100.1, 100.1, 5.3774*10^6, "Stock"}}
share|improve this question
    
Don't have Mma on the computer I am using atm but if you know the tickers then Cases[list,{"KO",__}] and Cases[list,{"MCD",__}] should do it for extracting. To recognize change in ticker SplitBy will probably be your friend. See docs. –  Mike Honeychurch Jul 22 '13 at 2:27
    
Okey, lets say that I want to use SplitBy data how would that look like given the data? –  ALEXANDER Jul 22 '13 at 2:38
    
Tried applying the Cases[AA,{"KO",__}], did not work. What could I have done wrong? –  ALEXANDER Jul 22 '13 at 2:59
    
There is a syntax error in the list. The first entry for McDonald has two curly brackets, i.e. {{"MCD",... It should have one. When this is corrected code for Cases works fine. –  ubpdqn Jul 22 '13 at 3:15
    
I have changed the list, it is currently working with the Cases[list,{"KO",}] and Cases[list,{"MCD",}] but its not working with SplitBy[list,#[[1,1]]&] –  ALEXANDER Jul 22 '13 at 3:28

1 Answer 1

list = {{"Symbol", "Full Name", "Date", "Open", "High", "Low", 
    "Close", "Adj Close", "Volume", "Type"}, {"KO", 
    "The Coca-Cola Company", {2013, 7, 19, 0, 0, 0.}, 40.88, 41.1, 
    40.79, 41.09, 41.09, 1.14011*10^7, "Stock"}, {"MCD", 
    "McDonald", {2013, 7, 19, 0, 0, 0.}, 100.2, 100.41, 99.53, 100.27,
     100.27, 4.5083*10^6, "Stock"}, {"MCD", 
    "McDonald", {2013, 7, 18, 0, 0, 0.}, 100.48, 100.77, 99.99, 
    100.18, 100.18, 3.4016*10^6, "Stock"}, {"MCD", 
    "McDonald", {2013, 7, 17, 0, 0, 0.}, 100.05, 100.35, 99.3, 100.1, 
    100.1, 5.3774*10^6, "Stock"}};

To extract ticker data:

Cases[list, {"KO", __}]
(*
{{"KO", "The Coca-Cola Company", {2013, 7, 19, 0, 0, 0.}, 40.88, 41.1,
   40.79, 41.09, 41.09, 1.14011*10^7, "Stock"}}
*)

Being able to "spot when the data moves from one ticker to another" is quit vague at present. You probably need to clarify what form this "spotting" should take but here is one way in which the "runs" of a particular ticker symbol are split:

SplitBy[list, First]
(*   
{{{"Symbol", "Full Name", "Date", "Open", "High", "Low", "Close", 
   "Adj Close", "Volume", "Type"}}, {{"KO", 
   "The Coca-Cola Company", {2013, 7, 19, 0, 0, 0.}, 40.88, 41.1, 
   40.79, 41.09, 41.09, 1.14011*10^7, "Stock"}}, {{"MCD", 
   "McDonald", {2013, 7, 19, 0, 0, 0.}, 100.2, 100.41, 99.53, 100.27, 
   100.27, 4.5083*10^6, "Stock"}, {"MCD", 
   "McDonald", {2013, 7, 18, 0, 0, 0.}, 100.48, 100.77, 99.99, 100.18,
    100.18, 3.4016*10^6, "Stock"}, {"MCD", 
   "McDonald", {2013, 7, 17, 0, 0, 0.}, 100.05, 100.35, 99.3, 100.1, 
   100.1, 5.3774*10^6, "Stock"}}}

*)

Or alternatively

Length /@ SplitBy[list, First]

(* {1, 1, 3} *)

...so we need some clarification about what you would like to see to spot when the data moves from one ticker symbol to another.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.