0

I'm attempting to transform a list of numbers a binary class based on the below parameters:

IF the value is greater than 3 THEN 0

IF the value is 3 or less than 3 THEN 1

Currently I've put together the below if else command. But this is coming back as an error.

if (df$DaysOverdue[df$daysoverdue == ">3"]) {
df$DaysOverdue[df$DaysOverdue] <- 0 
}
else {
df$DaysOverdue[df$DaysOverdue] <- 1 
}
  • 1
    Maybe just `df$DaysOverdue <- ifelse(df$DaysOverdue>3,0,1)`. See the `?ifelse` help page. A more [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) would be helpful. Doing an exact character match for ">3" seems odd. – MrFlick Oct 25 '17 at 19:06
  • 1
    Or maybe `df$DaysOverdue <- as.integer(df$DaysOverdue > 3)`. – lmo Oct 25 '17 at 19:09
  • 1
    There's a typo in the question, `daysoverdue/DaysOverdue`. – Rui Barradas Oct 25 '17 at 19:12

2 Answers2

1

Here's a simple solution using the tidyverse:

library(tidyverse)
df %>%
  mutate(`Overdue <= 3 Days` = as.numeric(DaysOverdue <= 3))
mavery
  • 101
  • 1
  • 4
0

It should be as easy as this:

df$DaysOverdueBin[df$DaysOverdue > 3] <- 0
df$DaysOverdueBin[df$DaysOverdue <= 3] <- 1
divibisan
  • 11,659
  • 11
  • 40
  • 58