Mathematica中的条件列表值替换 [英] Conditional list values replacement in Mathematica

查看:282
本文介绍了Mathematica中的条件列表值替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑:

dalist = Transpose@{{"Noise1",1,1,1,1,1},{"Blah", 1, 2, 3, 4, 5},
                   {"Noise2",2,2,2,2,2}, {"COGCondition", 1, 2, 1, 2, 1}}

COGCondition1 = 10
COGCondition2 = 20

鉴于 COGCondition列中使用的值,我想替换 Blah列中的值,使得:

I would like to replace the values in column "Blah" given the value taken in column "COGCondition" such that:

如果对于给定的行, COGCondition列中的值= 1,则 Blah列中的值应等于COGCondition1(并且2-> COGCondition2)

If, for a given row, the value in column "COGCondition" = 1 the value in column "Blah" should be equal to COGCondition1 (And 2 -> COGCondition2)

所需的输出:

对于大型数据集和使用 Table Switch 失败。我可以轻松生成新列,但无法弄清楚如何使用 Switch 替换值。

I need this for a large data set and my attempts using Table and Switch have failed. I can easily generate new columns but can't figure out how to replace values using Switch.

推荐答案

我会使用:

dalist[[2 ;;, 2]] =
 dalist[[2 ;;, 4]] /. {1 -> COGCondition1, 2 -> COGCondition2};

dalist //TableForm

如果修改了 dalist 是不需要的,您可以先复制它,例如 dalist2 = dalist 然后修改副本。

If modification of dalist is undesired, you can copy it first, e.g. dalist2 = dalist and then modify the copy.

特别是如果条件列的值很多,我建议您可以按照之前的建议使用索引变量( COGCondition [1] )。看起来像这样:

Especially if you have many values for the condition column, I suggest you follow the earlier recommendation to use an indexed variable (COGCondition[1]). This would look like:

dalist[[2 ;;, 2]] = COGCondition /@ dalist[[2 ;;, 4]];

这篇关于Mathematica中的条件列表值替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆