Mathematica中的条件列表值替换 [英] Conditional list values replacement in 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 $ c的尝试,我都需要使用它$ c>和
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屋!