# Generating a dummy variable

Posted 9 years ago
3765 Views
|
6 Replies
|
0 Total Likes
|
 In my original data, I have a n x 2 matrix. I now want to generate a new column, containing a dummy variable indicationg whether the element in column 1 is greater than the element in column 2. Alternatively, I managed to generate a new data file containing the difference between the two columns (And then adding it with the Join function). This new file contains values ranging from -6 up to 6. However, I did not manage to assign dummies to these values.The general idea is that I want a value of 1 is the element in the first column is larger, otherwise a 0 (Or likewise 1 if the element is positive in my own generated data, or 0 otherwise). The values of the two columns are never identical either.My question now is: how do generate a dummy variable for a whole column? I have the two above mentioned opportunities to create the dummy, but with neither data did I reach my goal.Thanks for your support.
6 Replies
Sort By:
Posted 9 years ago
 It worked perfectly, thank you very much Gianluca!
Posted 9 years ago
 You should wrap your data into a list {{6., 7.}, {6., 4.}, {1., 6.}, {4., 6.}, {7., 5.}, {6., 2.}, {6., 7.}, {6., 4.}} and then apply the replacement rule outside the final bracket:  {{6., 7.}, {6., 4.}, {1., 6.}, {4., 6.}, {7., 5.}, {6., 2.}, {6., 7.}, {6., 4.}}/. {a_, b_} :> {a, b, If[a >= b, 1, 0]} The repacement rule instructs Mathematica to replace any list of two elements of the form {a,b} with the triplet {a,b,variable}, where the variable is 1 or 0 according to the values of a,b. The underscores in {a_, b_} mean that a and b are not meant to be literally the two symbols a and b, but they are patterns to which we assign the tags a and b for the purposes of the replacement.The rule {a_, b_} :> {a, b, If[a >= b, 1, 0]} fails if your matrix is 2 by 2, in which case you should use the more elaborate version {a_?NumericQ, b_} :> {a, b, If[a >= b, 1, 0]} ,which checks if a is a number.There are other ways of solving your problem, that may be more obscure but a lot faster for very large matrices.
Posted 9 years ago
 It's a nx2 matrix, containing integers.i.e.:{6., 7.}, {6., 4.}, {1., 6.}, {4., 6.}, {7., 5.}, {6., 2.}, {6., 7.}, {6., 4.}
Posted 9 years ago
 What is the format of your matrix? Is it a list of lists? Can you give a small sample?
Posted 9 years ago
 Thanks for your reply. However, how do I apply this code?i.e. if i name my dataset "data", do I just typedata[{{1, 2}, {2, 1}} /. {a?NumericQ, b} :> {a, b, If[a >= b, 1, 0]}]?Sorry for the weird questions, I'm quite new to this program =)Thanks!
Posted 9 years ago
 Something like this? {{1, 2}, {2, 1}} /. {a_?NumericQ, b_} :> {a, b, If[a >= b, 1, 0]}