1

I have a column with tips in my dataframe and will try to run a logistic regression to predict if a tip will be left or now.

Trying to create a boolean column in my data, having 1 and 0, using mutate. 1 for a tip, 0 for no tip.

My code is pretty simple:

data %>% mutate(ifelse((Tips > 0, 1), ifelse(Tips == 0, 0))) 

As an output I have values, which go above 1, for instance, tip = 7.00 converts to boolean value 7, which is not what I expect.

Tips boolean
1.75    1
2.00    2
0.00    0
2.35    2
0.00    0
1.00    1
0.00    0
0.00    0
7.00    7
0.00    0

What am I missing? Thanks!

Anakin Skywalker
  • 2,400
  • 5
  • 35
  • 63

1 Answers1

3

Your code doesn't work but I guess you were trying to do something like this :

library(dplyr)
data %>% mutate(boolean = ifelse(Tips > 0, 1, ifelse(Tips == 0, 0, NA)))

Or if you have many conditions to check use case_when which is cleaner.

data %>% mutate(boolean = case_when(Tips > 0 ~ 1, 
                                    Tips == 0 ~ 0))

Assuming Tips would always have 0 or a positive value you don't need any ifelse at all.

data$boolean <- +(data$Tips > 0)

Or use sign which returns 0 for 0 values and 1 for any number greater than 0.

data$boolean <- sign(data$Tips)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213