Skip to content
Prev 392842 / 398502 Next

Converting a Date variable from character to Date

Try this by add a "day" to the date field

    library(tidyverse)
    library(lubridate)
    input <- "*Period            CPI*
    2022m1         4994
    2022m2         5336
    2022m3         5671
    2022m4         6532
    2022m5         7973
    2022m6        10365
    2022m7        12673
    2022m8        14356
    2022m9        14708"

    m_data <- read.delim(text = input, sep = "")

    # convert the date by adding a "day" before the conversion

    m_data$date <- ymd(paste0(m_data$X.Period, '-1'))
    m_data

    ##   X.Period  CPI.       date
    ## 1   2022m1  4994 2022-01-01
    ## 2   2022m2  5336 2022-02-01
    ## 3   2022m3  5671 2022-03-01
    ## 4   2022m4  6532 2022-04-01
    ## 5   2022m5  7973 2022-05-01
    ## 6   2022m6 10365 2022-06-01
    ## 7   2022m7 12673 2022-07-01
    ## 8   2022m8 14356 2022-08-01
    ## 9   2022m9 14708 2022-09-01



Thanks

Jim Holtman
*Data Munger Guru*


*What is the problem that you are trying to solve?Tell me what you want to
do, not how you want to do it.*


On Thu, Sep 29, 2022 at 9:36 AM Admire Tarisirayi Chirume <
atchirume at gmail.com> wrote: