1
$\begingroup$

I want to create DateListPlot that compares prices of cellphone manufacturers' stocks over the last 13 years. Stocks include Nokia, Blackberry, Samsung, LG, Motorola, and Sony-Ericsson. The problem is Samsung's and LG's stocks are in Korean Wons. I wrote a function that applies a UnitConvert to the price, it works with the a singular example, and small batches of data, but it refuses to run through the entire dataset. What could be the issue? Some errors in data? Should I clean it? Code:

sams1 = FinancialData["KS:005930", {2006, 1, 1}];

Convert[n_] := UnitConvert[Quantity[n, "KoreanWon"], "USDollars"]
ApplyFormat[list_] := {list[[1]], Convert[list[[2]]]}

ApplyFormat[sams1[[1]]]

ApplyFormat /@ sams1 (* This one refuses to work *)
$\endgroup$
  • $\begingroup$ What do you mean by "refuses to run"? Does it give an error? $\endgroup$ – Jason B. Jan 30 at 17:21
  • $\begingroup$ try Cases[sams1, {{a__}, b_} -> {{a}, Convert@b}] $\endgroup$ – Jerry Jan 30 at 17:27
5
$\begingroup$

You are calling UnitConvert[Quantity[n, "KoreanWon"], "USDollars"] for every value of n. UnitConvert might be making an internet query for every time you do a currency conversion, so if sams1 is a long list then you are making many such calls. In this case it's wasteful since you could just compute the conversion rate once and use it over again.

Use either

sams1[[All, 2]] *= UnitConvert[Quantity[1, "KoreanWon"], "USDollars"] 

or take advantage of the fact that UnitConvert and Quantity automatically thread over lists and use

sams1[[All,2]] = UnitConvert[Quantity[sams1[[All,2]], "KoreanWon"], "USDollars"]

either way, you only make one request for the conversion factor.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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