White Big - 7 months ago 55

R Question

I'm curious about how to solve this problem. I have data frame like this:

`a b`

1 0

1 0

0 1

1 1

0 0

1 0

Then the output has an extra column based on values of "a" and "b":

if(a==1&b==1) c=1, if(a==1&b==0) c=2, if(a==0&b==1) c=3, else c=4.

`a b c`

1 0 2

1 0 2

0 1 3

1 1 1

0 0 4

1 0 2

Any thoughts? I don't to write a for loop with nested if-else, how could we go vectorize? Thanks!

Source (Stackoverflow)