Date formatting MMM-YYYY

后端 未结 2 1892
耶瑟儿~
耶瑟儿~ 2021-01-24 16:51

I have a dataset with dates in following format:

Initial:

Jan-2015 Apr-2013 Jun-2014 Jan-2015 Jan-2016 Jan-2015 Jan-2016 Jan-2015 Apr-2012 Nov-2012 Jun-2         


        
2条回答
  •  遥遥无期
    2021-01-24 17:24

    To expand on @neilfws answer, you can use the month and year functions from the lubridate package to create your dummy variables with the month and year in your data frame.

    Here is the code:

    library(lubridate)
    library(zoo)
    df <- data.frame(Initial = c("Jan-2015", "Apr-2013", "Jun-2014", "Jan-2015", "Jan-2016", "Jan-2015", 
                             "Jan-2016", "Jan-2015", "Apr-2012", "Nov-2012", "Jun-2013", "Sep-2013"),
                 Final = c("Feb-2014", "Jan-2013", "Sep-2014", "Apr-2013", "Sep-2014", "Mar-2013", 
                           "Aug-2012", "Apr-2012", "Oct-2012", "Oct-2013", "Jun-2014", "Oct-2013"))
    
    df$Initial <- as.character(df$Initial)
    df$Final <- as.character(df$Final)
    df$Initial <- as.yearmon(df$Initial, "%b-%Y")
    df$Final <- as.yearmon(df$Final, "%b-%Y")
    df$month_initial <- month(df$Initial)
    df$year_intial <- year(df$Initial)
    df$month_final <- month(df$Final)
    df$year_final <- year(df$Final)
    
    df$Difference <- 12*(df$Initial-df$Final)
    

    And here is the final data.frame:

    > head(df)
       Initial    Final month_initial year_intial month_final year_final Difference
    1 Jan 2015 Feb 2014             1        2015           2       2014         11
    2 Apr 2013 Jan 2013             4        2013           1       2013          3
    3 Jun 2014 Sep 2014             6        2014           9       2014         -3
    4 Jan 2015 Apr 2013             1        2015           4       2013         21
    5 Jan 2016 Sep 2014             1        2016           9       2014         16
    6 Jan 2015 Mar 2013             1        2015           3       2013         22
    

    Hope this helps!

提交回复
热议问题