# How to replace the string

How to replace the string k with 1*^3 and Hz with blank space, such that I could use ListLogLog[]

A={12.5 Hz,16 Hz,20 Hz,25 Hz,31.5 Hz,40 Hz,50 Hz,63 Hz,80 Hz,100 Hz,125 Hz,160 Hz,200 Hz,250 Hz,315 Hz,400 Hz,500 Hz,630 Hz,800 Hz,1 kHz,1.25 kHz,1.6 kHz,2 kHz,2.5 kHz,3.15 kHz,4 kHz,5 kHz,6.3 kHz,8 kHz,10 kHz,12.5 kHz,16 kHz,20 kHz}/. {k -> 1*^3};
B={-10.12,-4.71,1.55,15.62,16.68,22.18,40.92,28.05,31.25,39.41,44.1,47.27,41.69,31.36,35.,39.51,40.82,42.2,42.04,42.76,41.99,41.17,40.15,39.24,36.25,36.39,34.53,30.76,33.91,39.56,44.69,35.1,18.13};
ListLogLogPlot[Transpose[{A, B}], Joined -> True]


## 4 Answers

Update: If elements of A are strings as in

a2 = {"12.5 Hz", "16 Hz", "20 Hz", "25 Hz", "31.5 Hz", "40 Hz",
"50 Hz", "63 Hz", "80 Hz", "100 Hz", "125 Hz", "160 Hz", "200 Hz",
"250 Hz", "315 Hz", "400 Hz", "500 Hz", "630 Hz", "800 Hz", "kHz",
"1.25 kHz", "1.6 kHz", "2 kHz", "2.5 kHz", "3.15 kHz", "4 kHz",
"5 kHz", "6.3 kHz", "8 kHz", "10 kHz", "12.5 kHz", "16 kHz",
"20 kHz"};


you can use use StringReplace + ToExpression as follows:

A3 = ToExpression@StringReplace[a2, {"k" -> "1000", "Hz" -> ""}];

ListLogLogPlot[Transpose[{A3, B}], Joined -> True]


Original answer:

You can use Quantity and UnitConvert:

A2 = UnitConvert[Quantity[ToString@#] & /@ A, "Hz"];
ListLogLogPlot[Transpose[{A2, B}], Joined -> True]


As noted by b3m2a1 in comments, A2 = UnitConvert[Quantity[ToString@#] & /@ A, "Hertz"] also works.

\$Version


11.3 .0 for Microsoft Windows (64 - bit) (March 7, 2018)

Also works in version 12.0.0 (Wolfram Cloud).

• EntityValue::conopen: Using EntityValue requires internet connectivity. Please check your network connection. You may need to configure your firewall program or set a proxy in the Internet Connectivity tab of the Preferences dialog.. I am getting this error Feb 1, 2020 at 7:53
• @acoustics replace the "Hz" with "Hertz" should fix it. Or fix your internet proxy. Feb 1, 2020 at 8:02
• @b3m2a1 The replacement is the issue here. I could not able to do the replacement? Feb 1, 2020 at 8:17
• @acoustics then Bills answer should fix it...? Use A/.{kHz->1*^3,Hz->1} Feb 1, 2020 at 8:27
• @acoustics there was no indication in your question that each element of the list was a string. In the future please make something like that clear as that would make it really easy to provide an answer. Feb 1, 2020 at 21:13

I don't see any quotes in that, perhaps they were lost in the process.

Try

A={12.5 Hz,1.25 kHz}/.{kHz->1*^3,Hz->1}


and that should replace Hz and kHz.

If there are supposed to be quotes then try

A={12.5 "Hz",1.25 "kHz"}/.{"kHz"->1*^3,"Hz"->1}

• Actually I have a huge amount of data like what I have posted in OP. I cannot manually put quotes. Is there any other ways? Feb 1, 2020 at 7:11
• I showed two alternatives, one should work if you don't have quotes and the other should work if you do have quotes. Can you try the appropriate version for the form you have and see if it works? You can also do FullForm[{...one line of your data...}] and that should avoid hiding any quotes that might be there and show you the actual characters.
– Bill
Feb 1, 2020 at 7:35

Maybe try converting kHz to 1000 Hz (so that now everything is the same unit-wise) and then using part (to get the Integer or Real part of each entry),as follows:

{your,data,list}/. {kHz :> 1*^3 Hz})[[All,1]];


For Example:

A=({12.5 Hz,16 Hz,20 Hz,25 Hz,31.5 Hz,40 Hz,50 Hz,63 Hz,80 Hz,100 Hz,125 Hz,160 Hz,200 Hz,250 Hz,315 Hz,400 Hz,500 Hz,630 Hz,800 Hz,1 kHz,1.25 kHz,1.6 kHz,2 kHz,2.5 kHz,3.15 kHz,4 kHz,5 kHz,6.3 kHz,8 kHz,10 kHz,12.5 kHz,16 kHz,20 kHz}/. {kHz :> 1*^3 Hz})[[All,1]];
B={-10.12,-4.71,1.55,15.62,16.68,22.18,40.92,28.05,31.25,39.41,44.1,47.27,41.69,31.36,35.,39.51,40.82,42.2,42.04,42.76,41.99,41.17,40.15,39.24,36.25,36.39,34.53,30.76,33.91,39.56,44.69,35.1,18.13};
ListLogLogPlot[Transpose[{A, B}], Joined -> True]


Update

A[[20]] is just 'kHz' (with the Head of Symbol). If this was 1 kHz (now, like all other entries, with the Head of Real Symbol), then you could probably use Part without any transformation rule.

(Head/@A)[[1]]


Times

(Head/@A)[[20]]


Symbol

Head/@A[[1]]


Real Symbol

ToString and ToExpression is a alternative method.

 A = {12.5 Hz, 16 Hz, 20 Hz, 25 Hz, 31.5 Hz, 40 Hz, 50 Hz, 63 Hz,
80 Hz, 100 Hz, 125 Hz, 160 Hz, 200 Hz, 250 Hz, 315 Hz, 400 Hz,
500 Hz, 630 Hz, 800 Hz, 1 kHz, 1.25 kHz, 1.6 kHz, 2 kHz, 2.5 kHz,
3.15 kHz, 4 kHz, 5 kHz, 6.3 kHz, 8 kHz, 10 kHz, 12.5 kHz,
16 kHz, 20 kHz} // ToString /@ # & //
StringReplace[#, {"kHz" -> "1*^3", "Hz" -> " "}] & // ToExpression


In this situation, it maybe not so simple. But in some cases, to avoid calculate midway, String is a good way.