1

apologies if this has been answered, but I've looked for the last hour and cannot find a simple answer to my question.

Very simply - I want to change / replace a value in a data frame observation based on a date conditional.

Example - (this is wrong but for illustration)

HR <-replace(HR$Status, HR$Call.Date < as.Date("01/01/2019"), "closed")

Where I want to replace the HR$Status value with "closed" if the HR$Call.Date is "before" (less than) 01/01/2019. Any help is appreciated or if there is a current open question for this.

Thanks in advance

Matt
  • 37
  • 1
  • 8
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. What's wrong with your example code? Is `HR$Status` a character or a factor column? If the latter, is "closed" a valid level in that factor? – MrFlick Feb 25 '19 at 20:09
  • Got it - I will do that moving forward. I couldnt "get it to work" is why i didnt include it – Matt Feb 25 '19 at 21:24
  • Typically this sort of question (assignment of character value "won't work") will be foundering on a lack of recognition that the target column is a factor that does not include a level corresponding to the desired assignment. – IRTFM Feb 25 '19 at 22:29

1 Answers1

0

Let's assume Status is of character class and Call.Date is Date class - you can do what you need to with tidyverse:

library(tidyverse)
HR %>%
  mutate(Status = if_else(Call.Date < as.Date("01/01/2019"), "closed", Status))
nycrefugee
  • 1,629
  • 1
  • 10
  • 23