Date 如何在不删除“的情况下进行数学运算”;不适用;mathematica中的时间序列数据
我是Mathematica的新手,目前正在处理时间序列数据 我在Mathematica 9.0中有以下输入数据Date 如何在不删除“的情况下进行数学运算”;不适用;mathematica中的时间序列数据,date,wolfram-mathematica,Date,Wolfram Mathematica,我是Mathematica的新手,目前正在处理时间序列数据 我在Mathematica 9.0中有以下输入数据 data = {{{2011, 3, 13}, 10}, {{2011, 3, 14}, "NA"}, {{2011, 3, 15}, 20}, {{2011, 3, 16}, 30}, {{2011, 3, 17}, "NA"}}; 一旦我删除了“NA”,数据就变为 data = {{{2011, 3, 13}, 10}, {{2011, 3, 15}, 20}
data = {{{2011, 3, 13}, 10}, {{2011, 3, 14}, "NA"}, {{2011, 3, 15},
20}, {{2011, 3, 16}, 30}, {{2011, 3, 17}, "NA"}};
一旦我删除了“NA”,数据就变为
data = {{{2011, 3, 13}, 10}, {{2011, 3, 15},
20}, {{2011, 3, 16}, 30}}
我想做数学运算,比如加号、减号和平均值,而不删除“NA”(我想保持日期的顺序)
例如,如果我想用数据的平均值除以这些值,
我的输出可能是
data = {{{2011, 3, 13}, 0.5}, {{2011, 3, 14}, "NA"}, {{2011, 3, 15},
1}, {{2011, 3, 16}, 1.5}, {{2011, 3, 17}, "NA"}};
Mathematica有什么方法可以做到这一点吗?或者有什么建议? 你可以考虑使用<代码>不确定的< /代码>代替<代码>“NA”< /代码>。当与数字量组合时,其计算结果为不确定,例如:
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}}
*)
1 + Indeterminate
2 * Indeterminate
这简化了程序。这里使用除以
:
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
您使用
未定义的而不是将其保留为“NA”
或使用缺少[“NotAvailable”]
?@Mr.Wizard未定义的/x==未定义的所有x。我认为这比“NA”/x更干净,对不起,我没有意识到未定义的是v8中的系统符号+1.
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}}