Format Date as Year/Quarter

Format date as Year/Quarter

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"

Convert date to year-quarter format

You can use yearqtr in the zoo package.

> as.yearqtr(Sys.Date())
[1] "2012 Q4"

convert string to date (format year quarter) in pyspark

I think there is no direct function/format that will return quarter date.

We need to use when statement (or) udf for this case.

Example:

df=spark.createDataFrame([("1","2016/2017 Q2"),("2","2017/2018 Q1"),("3","2018/2019 Q3"),("4","2019/2020 Q4")],["id","dt"])

#4 quarters in an year
df.withColumn("date",
when(lower(reverse(split(col("dt")," "))[0]) == "q1",concat_ws("-",substring(col("dt"),0,4),lit("01-01")).cast("date")).\
when(lower(reverse(split(col("dt")," "))[0]) == "q2",concat_ws("-",substring(col("dt"),0,4),lit("04-01")).cast("date")).\
when(lower(reverse(split(col("dt")," "))[0]) == "q3",concat_ws("-",substring(col("dt"),0,4),lit("07-01")).cast("date")).\
when(lower(reverse(split(col("dt")," "))[0]) == "q4",concat_ws("-",substring(col("dt"),0,4),lit("10-01")).cast("date")).\
otherwise(lit("Quarter not found"))).show()

#+---+------------+----------+
#| id| dt| date|
#+---+------------+----------+
#| 1|2016/2017 Q2|2016-04-01|
#| 2|2017/2018 Q1|2017-01-01|
#| 3|2018/2019 Q3|2018-07-01|
#| 4|2019/2020 Q4|2019-10-01|
#+---+------------+----------+

Convert character in format YEARQT to a quarterly date in R

You almost got it! The format needs to be sligthly adapted.

%YQ%q: %Y stands for the year, Q stands for the Q in your inital format and %q stands for the quarter.

Code

library(zoo)

DATA.QTR <- DATA.QTR %>% mutate(QUARTER = as.Date(as.yearqtr(format(Date), "%YQ%q")))

Output

> DATA.QTR
# A tibble: 6 x 2
Date QUARTER
<chr> <date>
1 1960Q1 1960-01-01
2 1960Q2 1960-04-01
3 1960Q3 1960-07-01
4 1960Q4 1960-10-01
5 1961Q1 1961-01-01
6 1961Q2 1961-04-01

Data

DATA.QTR <- structure(list(Date = c("1960Q1", "1960Q2", "1960Q3", "1960Q4", 
"1961Q1", "1961Q2")), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -6L))

Convert quarter/year format to a date

1) The zoo package has a "yearqtr" class. Convert to that and then to "Date" class:

library(zoo)
x <- c("Q1/13","Q2/14")

as.Date(as.yearqtr(x, format = "Q%q/%y"))
## [1] "2013-01-01" "2014-04-01"

2) Alternately use this to get the last day of the quarter instead of the first:

as.Date(as.yearqtr(x, format = "Q%q/%y"), frac = 1)
## [1] "2013-03-31" "2014-06-30"

3) Also consider not converting to "Date" class at all and just using "yearqtr" class directly:

as.yearqtr(x, format = "Q%q/%y")
## [1] "2013 Q1" "2014 Q2"

Turn character YYYY-Q1 into date

You have to use the right format in as.yearqtr formula like this:

library(zoo)
x<-"2010-Q1"
as.yearqtr(x, format = "%Y-Q%q")
[1] "2010 Q1"


Related Topics



Leave a reply



Submit