Message Boards Message Boards

GROUPS:

Perform calculations with the $ symbol within the elements a dataset?

Posted 2 years ago
3450 Views
|
2 Replies
|
1 Total Likes
|

Hello Wolfram Mathematica community,

I've been playing around with Mathematica and I've isolated a set of data on a website; however, there are money signs within the data set. Is it possible to still perform mathematical processes with the $ symbol within the elements in the set? I tried and it thought the money sign was a variable.

2 Replies
Posted 2 years ago

William,

The problem is that the data is imported as strings.

Part[data2, 2, 2, 2, 2, 1] // InputForm
(* {"--", "Max Scherzer", "WSH", "SP", "$42,142,857", "7 (2015-21)", "$210,000,000", "$30,000,000"} *)

To convert to numbers, remove the $ and , and then ToExpression

data = Part[data2, 2, 2, 2, 2, All, 7] //
  StringReplace[{"$" -> "", "," -> ""}] // Map[ToExpression];

Compute mean

N@Mean[data]

Or use a currency interpreter:

data=Import["https://www.usatoday.com/sports/mlb/salaries/",{"Data"}];
salaryStrings=data[[2,2,2,2,All,5]];
salaries=Interpreter["CurrencyAmount"]@salaryStrings//Cases[#,_Quantity]&;
Mean@salaries//N
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract