Message Boards Message Boards

0
|
7477 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
Share this post:

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

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.

POSTED BY: William Ferguson
2 Replies
Posted 5 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]
POSTED BY: Rohit Namjoshi

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
POSTED BY: Vincent Virgilio
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