Format date as Year/Quarter

前端 未结 8 1359
隐瞒了意图╮
隐瞒了意图╮ 2020-12-03 14:06

I have the following dataframe:

Data <- data.frame(
  date = c(\"2001-01-01\", \"2001-02-01\", \"2001-03-01\", \"2001-04-01\", \"2001-05-01\", \"2001-06-0         


        
相关标签:
8条回答
  • 2020-12-03 14:12

    Another (longer) way of doing it using if statements is this:

    month <- as.numeric(format(date, format = "%m"))[1]
    
    if (month < 4) {
        quarter <- paste( format(date, format = "%Y")[1], "Q1", sep="-")
    } else if (month > 3 & month < 7) {
        quarter <- paste( format(date, format = "%Y")[1], "Q2", sep="-")            
    } else if (month > 6 & month < 10) {
        quarter <- paste( format(date, format = "%Y")[1], "Q3", sep="-")
    } else if (month > 9) {
        quarter <- paste( format(date, format = "%Y")[1], "Q4", sep="-")
    }
    

    Returns a string in the format:

    > quarter
    [1] "2001-Q1"
    

    Then you could extend that using a loop.

    0 讨论(0)
  • 2020-12-03 14:15

    Using base functions:

    Data$date <- as.Date(Data$date)
    Data$qtr <- paste(format(Data$date, "%y"), 
                      sprintf("%02i", (as.POSIXlt(Data$date)$mon) %/% 3L + 1L), 
                      sep="/")
    
    #         date   qtr
    # 1 2001-01-01 01/01
    # 2 2001-02-01 01/01
    # 3 2001-03-01 01/01
    # 4 2001-04-01 01/02
    # 5 2001-05-01 01/02
    # 6 2001-06-01 01/02
    
    0 讨论(0)
  • 2020-12-03 14:15

    I have been loving the lubridate package for working with dates. Super slick. The quarter function finds the quarter (of course) and then just pair that with the year.

    library(lubridate)
    Data <- Data %>%
      mutate(qtr = paste0(substring(year(date),2,4),"/0",quarter(date))) 
    

    If you are not familiar with the %>% the first line basically says "use data frame called data" and the second line says "mutate (or add) a column called qtr"

    0 讨论(0)
  • 2020-12-03 14:21
    yq=function(x,prefix="%Y",combine="Q") paste0(ifelse(is.null(prefix),"",format(x,"%Y")),floor(as.numeric(format(x,"%m"))/3-1e-3)+1,sep=combine)
    

    this gives the flexibility of returning any format back that has the quarter in it

    no need for chron or zoo

    as for your example

    yq(as.Date("2013-04-30"),prefix="%y",combine="/0")
    > [1] "13/02"
    
    0 讨论(0)
  • 2020-12-03 14:22

    I made a similar format using quarters() and sub() in R:

    Data$qtr <- paste(format(Data$date, "%y/"), 0, 
                      sub( "Q", "", quarters(Data$date) ), sep = "")
    
    0 讨论(0)
  • 2020-12-03 14:23

    You need to explicilty Vectorize your function:

    fun_v <- Vectorize(fun, "x")
    fun_v(Data$date)
    #[1] "01/01" "01/01" "01/01" "01/02" "01/02" "01/02"
    

    However, when it comes to more or less standard tasks (such as datetime manipulations), there's always a solution already available:

    library(zoo)
    yq <- as.yearqtr(Data$date, format = "%Y-%m-%d")
    yq
    #[1] "2001 Q1" "2001 Q1" "2001 Q1" "2001 Q2" "2001 Q2" "2001 Q2"
    

    To convert to your specific format, use

    format(yq, format = "%y/0%q")
    #[1] "01/01" "01/01" "01/01" "01/02" "01/02" "01/02"
    
    0 讨论(0)
提交回复
热议问题