Home > other >  converting a column from character to rdate format. But with some values already in Rdate format
converting a column from character to rdate format. But with some values already in Rdate format

Time:05-07

enter image description here

This column is from a dataframe called bdata. How do I convert this column smoothly into an rdate format when some of the variables here are already in rdate format.

bdata$Period <- as.Date(bdata$Period,"%Y/%m/%d")
bdata

This result already comes up with NA values.

CodePudding user response:

What you can do is first format the first 8 dates and after that the last 2 dates in two different calls. After that, you can merge them into one column. First I created a subset of your data. Now you can make the right formats:

df <- data.frame(date = c("26/04/2022", "27/04/2022", "2020-03-01", "2020-03-02"))

df$date2 <- as.Date(df$date , format = "%d/%m/%y")
df$date3 <- as.Date(df$date, format = "%Y-%m-%d")

        date      date2      date3
1 26/04/2022 2020-04-26       <NA>
2 27/04/2022 2020-04-27       <NA>
3 2020-03-01       <NA> 2020-03-01
4 2020-03-02       <NA> 2020-03-02

Now you can merge the date2 and date3 column using this code:

df$date <- do.call(pmin, c(df[c("date2","date3")], na.rm=TRUE))

        date      date2      date3
1 2020-04-26 2020-04-26       <NA>
2 2020-04-27 2020-04-27       <NA>
3 2020-03-01       <NA> 2020-03-01
4 2020-03-02       <NA> 2020-03-02

As you can see the date column is now in the right column. At last you can select that column and you have your column:

library(dplyr)

df <- df %>% select(date)

        date
1 2020-04-26
2 2020-04-27
3 2020-03-01
4 2020-03-02

As you can see the column is in the right format.

CodePudding user response:

Update: Even more straight using parse_date from parsedate package:

library(parsedate)
library(dplyr)
df %>% 
  mutate(new_col = parse_date(Period))

We could use parse_date_time the trick is to give the order of the different formats:

library(dplyr)
library(lubridate)
df %>% 
  mutate(new_col = ymd(parse_date_time(Period, orders = c('dmy', 'ymd'))))

With the data from Quinten(many thanks):

      Period    new_col
1 26/04/2022 2022-04-26
2 27/04/2022 2022-04-27
3 2020-03-01 2020-03-01
4 2020-03-02 2020-03-02
  • Related