-3

I want to make a new variable "churned" by taking into account five variables :

  1. Include in churn
  2. A-Churn
  3. B-Churn
  4. C-Churn
  5. D-Churn

My condition is - If variable "Include in churn" has 1 and for all other variables , if any one of the variables has 1 than my new variable "Churned" should have 1 else 0. I am a newbie in using mutate function.

Please help me to create this new variable thru 'mutate' function.

zx8754
  • 52,746
  • 12
  • 114
  • 209
anant saxena
  • 61
  • 1
  • 3
  • 9
  • 3
    Please show some data : http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – scoa Jan 09 '17 at 11:30
  • 2
    It sounds like you want to nest an `ifelse()` function within `mutate()` access their help docs by submitting `?ifelse` or `?mutate` to the console – DataJack Jan 09 '17 at 11:36

1 Answers1

1

If I understand your formulation logically, you want

mutate(data, Churned = Include.in.Churn == 1 & (A.Churn == 1 | B.Churn == 1 | C.Churn == 1 | D.Churn == 1))

This will make Churned a logical. If you really need an integer, as.integer will produce 1 for TRUE and 0 for FALSE.

If all mentioned Variables are either 1 or 0 you can also use the possibly faster

mutate(data, Churned = Include.in.Churn * (A.Churn + B.Churn + C.Churn + D.Churn) >= 1)
AlexR
  • 2,412
  • 16
  • 26