Home > Back-end >  Convert character variable to date variable in R
Convert character variable to date variable in R

Time:10-06

I am trying this code (and others) but it never seems to update the variable type or if it does update to a date variable it applied the same date to each observation.

Any suggestions on how to fix?

DF %<>% as.Date(DF$date, "%d/%m/%Y")

Thanks!

CodePudding user response:

The question is not clear. Can you post the reproducible code?

Try this:

library(tidyverse)
DF <- data.frame(myDate = "06/10/2021")

DF %>%
  as_tibble() %>%
  mutate(newDate = as.Date(myDate, "%d/%m/%Y"))

gives

# A tibble: 1 × 2
  myDate     newDate   
  <chr>      <date>    
1 06/10/2021 2021-10-06

CodePudding user response:

If I understand your problem correctly you are trying to overwrite the whole Dataframe.

Try to directly edit the date-column:

DF$date <- as.Date(DF$date, "%d/%m/%Y")

  • Related