1
$\begingroup$

Hi I'd like to turn each non zero value of my selected columns to a 1 using mutate_at()

 BRAND  MEDIA_TYPE    INV1       INV2
   <chr>  <chr>      <dbl>     <dbl>
  b1     newspapers    2         27
  b1     magazines     3         0
  b2     newspapers    0         0
  b3     tv            1        145
  b4     newspapers    4         40
  b5     newspapers    5         0
  b1     newspapers    1         0
  b2     newspapers    0         28

The final result should be like follow:

 BRAND  MEDIA_TYPE    INV1      INV2
   <chr>  <chr>      <dbl>     <dbl>
  b1     newspapers    1         1
  b1     magazines     1         0
  b2     newspapers    0         0
  b3     tv            1         1
  b4     newspapers    1         1
  b5     newspapers    1         0
  b1     newspapers    1         0
  b2     newspapers    0         1

Do you have any suggestion on how to solve it? Thank you!!

$\endgroup$
  • $\begingroup$ The question headline is a little misleading, since this is not what the accepted answer is doing. $\endgroup$ – dule arnaux Jun 12 at 19:23
1
$\begingroup$
mutate_at(my_data, vars(starts_with("INV")), sign)

Although I see your INV1 column is a date? I'm not sure how much that'll affect things..

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.