Please consider :
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
I would like to replace the values in column "Blah" given the value taken in column "COGCondition" such that:
If, for a given row, the value in column "COGCondition" = 1 the value in column "Blah" should be equal to COGCondition1 (And 2 -> COGCondition2)
Desired output:
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
.
I would use:
If modification of
dalist
is undesired, you can copy it first, e.g.dalist2 = dalist
and then modify the copy.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:This is fairly straightforward with a replacement rule:
gives
Alternatively, you could use
MapThread
which returns
Edit: In your updated version of
dalist
, you have four columns: noise, data, noise, and condition. The update to the pattern version is simplyUnfortunately, this is somewhat fragile because it requires a bit of extra work if you change the number of conditions. The method suggested by Leonid, was to create a function
then this simplifies the update code
Alternatively, you could create a list of rules
then the code for changing
dalist
becomesIf you find that you have more than 1 column between
x
andy
, then your pattern is simply{a_, x_, b___, y_Integer}
. Or, if the number of columns prior tox
is larger than one, use{a___, x_, b_, y_Integer}
. However, they don't work together because Mathematica needs to know wherex
andy
are relative to some point in the list, or it won't operate as you expect, if at all.But, if you know the number of columns, you can use
PatternSequence
. For example, if you have 3 columns of noise, your data, 5 columns of noise, and then you condition, your replacement rule would beNow,
PatternSequence@@Array[_&,3]
could be writtenPatternSequence[_, _, _]
, but by usingArray
it gives more flexibility.Edit: One difficulty with either the indexed variable form,
COGCondition[n]
, or the rule form is if the condition column contains values other than 1 or 2. The simplest solution is to set up a default value, e.g.or add to
conditions
One possibility is to emit a
Message
whenever this default is encountered which would provide feed back as its running.Another possibility is to have the data column remain untouched if the default is encountered. To accomplish this, we can use the following
which would be used like
To make this work with the rule implementation, we make
conditions
a function which accepts the current data valuewhich changes the code for updating
dalist
toNote, either of the last two methods can be combined with emitting a
Message
from above.