How do I mutate a date using logic?

I am new to R and have only limited knowledge. I have a list of dates here that I would like to mutate . If the "Date" is less than the current date I would like the "Date" value to be replaced with "NA" in the same column. I tried using the mutate function, but I could not figure out the logic to make this code run.

Start <- structure(list(Date = structure(c(1620345600, 1621123200, 1624752000, 
                                   1625184000, NA, 1625356800, 1627084800, 1628035200, 1628985600, 
                                   1631404800, 1631577600, 1627344000, 1632182400, 1627430400, 1628985600, 
                                   1632441600, 1632441600, 1631145600, 1634083200, 1632441600, 1630195200
), class = c("POSIXct", "POSIXt"), tzone = "UTC")), class = c("tbl_df", 
                                                              "tbl", "data.frame"), row.names = c(NA, -21L))

current_date <- Sys.Date()
mutate(Date=ifelse(Date < current_date, Date, NA))

When running this code, I get the following error message.

Error in `<.default`(Date, current_date) : 
  comparison (3) is possible only for atomic and list types
Using the following data, I tried the following code but could not get it to work. 

Thank you for your help!

You didn't reference your initial data frame Start.

llibrary(dplyr)
Start %>% 
  mutate(Date = ifelse(Date < current_date, Date, NA)) 

Accounting for that embarrassing mistake, the code I ran is below.

Start <- structure(list(Date = structure(c(1620345600, 1621123200, 1624752000, 
                                           1625184000, NA, 1625356800, 1627084800, 1628035200, 1628985600, 
                                           1631404800, 1631577600, 1627344000, 1632182400, 1627430400, 1628985600, 
                                           1632441600, 1632441600, 1631145600, 1634083200, 1632441600, 1630195200
), class = c("POSIXct", "POSIXt"), tzone = "UTC")), class = c("tbl_df", 
                                                              "tbl", "data.frame"), row.names = c(NA, -21L))

current_date <- Sys.Date()
Start%>%
mutate(Date=ifelse(Date < current_date, Date, NA))
as.Date(Start$Date)
View(Start)

Running this code, I get the following output which unfortunately still does not replace the desired values with NA. I would like any date before today to have the value "NA".

structure(list(Date = structure(c(1620345600, 1621123200, 1624752000, 
1625184000, NA, 1625356800, 1627084800, 1628035200, 1628985600, 
1631404800, 1631577600, 1627344000, 1632182400, 1627430400, 1628985600, 
1632441600, 1632441600, 1631145600, 1634083200, 1632441600, 1630195200
), class = c("POSIXct", "POSIXt"), tzone = "UTC")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -21L))

Thank you for your help!

Assign it to a new variable. You are just mutating Start but not saving it.

# mutate then save as new object
new_start <- Start %>%
  mutate(Date=ifelse(Date < current_date, as.Date(Date), NA))

# just viewing new start without saving the filter
new_start %>% 
   filter(is.na(Date))
# A tibble: 6 × 1
   Date
  <dbl>
1    NA
2    NA
3    NA
4    NA
5    NA
6    NA

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.