data = {{{2011, 3, 13}, 10}, {{2011, 3, 14}, "NA"}, {{2011, 3, 15}, 20},
{{2011, 3, 16}, 30}, {{2011, 3, 17}, "NA"}} /. "NA" -> Undefined;
mean = Mean@Cases[data[[All, 2]], Except[Undefined]];
{#[[1]], #[[2]]/mean} & /@ data
(*
{{{2011, 3, 13}, 1/2}, {{2011, 3, 14}, Undefined},
{{2011, 3, 15}, 1}, {{2011, 3, 16}, 3/2},
{{2011, 3, 17}, Undefined}}
*)
how to do math operations without removing "NA" in time series data in mathematica
-
03-10-2022 - |
سؤال
I am new to Mathematica and currently working with time series data.
I have the following input data in Mathematica 9.0
data = {{{2011, 3, 13}, 10}, {{2011, 3, 14}, "NA"}, {{2011, 3, 15},
20}, {{2011, 3, 16}, 30}, {{2011, 3, 17}, "NA"}};
Once, I remove the "NA", the data become
data = {{{2011, 3, 13}, 10}, {{2011, 3, 15},
20}, {{2011, 3, 16}, 30}}
I want to do the math operation, such as plus, minus, and mean, without removing "NA" (I want to keep the sequence of date).
For example, in case that I want to divide the values with mean of the data, my output could be
data = {{{2011, 3, 13}, 0.5}, {{2011, 3, 14}, "NA"}, {{2011, 3, 15},
1}, {{2011, 3, 16}, 1.5}, {{2011, 3, 17}, "NA"}};
Are there any ways that Mathematica can do it? or any suggestions?
المحلول
نصائح أخرى
You might consider using Indeterminate
in place of "NA"
. When combined with a numeric quantity it evaluates to Indeterminate
, e.g.:
1 + Indeterminate
2 * Indeterminate
Indeterminate Indeterminate
This simplifies tensor operations. Here using DivideBy
:
data = {{{2011, 3, 13}, 10}, {{2011, 3, 14}, "NA"}, {{2011, 3, 15}, 20},
{{2011, 3, 16}, 30}, {{2011, 3, 17}, "NA"}};
data = data /. "NA" -> Indeterminate;
data[[All, 2]] /= Mean @ Select[data[[All, 2]], NumericQ];
data
{{{2011, 3, 13}, 1/2}, {{2011, 3, 14}, Indeterminate}, {{2011, 3, 15}, 1}, {{2011, 3, 16}, 3/2}, {{2011, 3, 17}, Indeterminate}}
لا تنتمي إلى StackOverflow