This is probably an easy question to answer, but I need a fresh eye... I'm struggling with processing the following file (extract shown here) It's a list of dates, and positive/negative time durations formatted using "m" and "s".
Apr-09 -1m 47s
Apr-11 -1m 15s
Apr-12 -0m 59s
Apr-13 -0m 44s
Apr-14 -0m 28s
Apr-15 -0m 13s
Apr-16 0m 1s
Apr-17 0m 15s
Apr-18 0m 29s
Apr-19 0m 42s
Apr-20 0m 55s
Apr-21 1m 8s
I'm reading the file in OK:
d = ReadList["/tmp/data.txt",
{Word, Number, Character, Number, Character}];
and assembling the pieces using this:
d /. {s_String, m_Integer, _, n_Integer, _} :> {s,
N@If[m >= 0, (60 m + n)/60, (60 m - n)/60]}
which appears to work:
{"Apr-09", -1.78333},
{"Apr-11", -1.25},
{"Apr-12", 0.983333},
{"Apr-13", 0.733333},
{"Apr-14", 0.466667},
{"Apr-15", 0.216667},
{"Apr-16", 0.0166667},
{"Apr-17", 0.25},
{"Apr-18", 0.483333},
{"Apr-19", 0.7},
{"Apr-20", 0.916667},
{"Apr-21", 1.13333}
but then I noticed that the 0s lost their signs on import, so the conversion is inaccurate:
d
{{"Apr-09", -1, "m", 47, "s"}, {"Apr-11", -1, "m", 15, "s"}, {"Apr-12", 0, "m", 59, "s"}, {"Apr-13", 0, "m", 44, "s"}, {"Apr-14", 0, "m", 28, "s"}, {"Apr-15", 0, "m", 13, "s"}, {"Apr-16", 0, "m", 1, "s"}, {"Apr-17", 0, "m", 15, "s"}, {"Apr-18", 0, "m", 29, "s"}, {"Apr-19", 0, "m", 42, "s"}, {"Apr-20", 0, "m", 55, "s"}, {"Apr-21", 1, "m", 8, "s"}}
The problem is that ReadList
's Number
reads "-0" as "0". Is there a better way of reading in data like this? I tried using Sign
but unfortunately Sign[-0]
gives 0... :)
ReadList
. I could try reading asWord
, I suppose. But the problem is then moved elsewhere rather than solved... $\endgroup$