0 votes
1 view
in R Programming by (43.2k points)

I am having a column that is having the tips and will try to build a logistic regression model to predict if a tip will be left or now.

I have created a boolean column in my data, having 1 and 0, using mutate() function:

1 for a tip, 0 for no tip.

My code is pretty simple:

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

I have values, which will go above 1, for example, tip = 7.00 converts to boolean value 7, which is not what I anticipate.

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

1 Answer

0 votes
by (92.8k points)
edited by

I think your code will not work as 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 requirements to check, then you can use case_when which is cleaner.

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

                                    Tips == 0 ~ 0))

If you do not want to use the ifelse at all then you have to assume the Tips to have 0 or some positive integer value.

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)

If you are looking for the R certification, then do check out the R programming certification

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...