1
$\begingroup$

I have a data set that is something like:

time={-2_ms, -1_ms, 0_ms, 1_ms, 2_ms};

How do I drop the _ms part such that its just a list of numbers?

$\endgroup$
4
  • 4
    $\begingroup$ Are those strings in your dataset? You can't have underscores in data or variable names: they have a predefined meaning in MMA (they refer to Blank patterns). How are you generating or importing the dataset? $\endgroup$
    – MarcoB
    Feb 28 at 16:34
  • $\begingroup$ Yes, I don't want underscores in my data. As stated in the question, I just want the values. The data is imported and I can't control how the data is exported. $\endgroup$
    – Alex Wong
    Feb 28 at 16:56
  • 1
    $\begingroup$ OK got it, however the way you import the data is critical here. Can you please share the original form in which the data is given to you, and the code you used to import it? $\endgroup$
    – MarcoB
    Feb 28 at 17:00
  • 4
    $\begingroup$ We understand that you want to remove the underscores. But information often gets lost when people take stuff from their MMA notebook and paste it here. For example, is the expression really "{-2_ms,-1_ms,0_ms,1_ms,2_ms}"? Or is it {"-2_ms", "-1_ms", "0_ms", "1_ms", "2_ms"}? Or is it List[Times[-2, Blank[ms]], Times[-1, Blank[ms]], 0, Blank[ms], Times[2, Blank[ms]]]? Also, this is a very strange expression--it's almost certainly not a semantically valid representation even though it doesn't generate errors. So, if you give us more context, we can do a better job of helping you. $\endgroup$
    – lericr
    Feb 28 at 17:30

2 Answers 2

3
$\begingroup$

Perhaps

list = {"-2_ms", "-1_ms", "0_ms", "1_ms", "2_ms"};
Map[StringReplace[#, "_ms" -> ""] &, list] // ToExpression 
(*{-2, -1, 0, 1, 2}*)

or

Map[StringDelete[#, "_ms" ] &, list] // ToExpression
$\endgroup$
2
$\begingroup$
list = {"-2_ms", "-1_ms", "0_ms", "1_ms", "2_ms"};

StringSplit[list, "_"] // Map[First] // ToExpression

{-2, -1, 0, 1, 2}

$\endgroup$

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