I want to combine two variables into one with a date format

后端 未结 5 886
再見小時候
再見小時候 2021-01-26 22:59

I have a data set with a character column for months (MONTH) and a numeric column indicating years (YEAR). In order to work with it as panel data, I ne

5条回答
  •  盖世英雄少女心
    2021-01-26 23:30

    Combining Tim's response with an easy to use date package lubridate we get:

    # This can handle months of JAN, FEB, ETC. Or it can handle months of 01,02,etc.
    df$TIME <- lubridate::ymd(paste0(df$YEAR,df$MONTH,"01")) 
    
    # or if you need it in MM-YYYY format:
    df$TIME <- format(lubridate::ymd(paste0(df$YEAR,df$MONTH,"01")), "%m-%Y")
    

提交回复
热议问题