I have panel of quarterly data which looks like this:
x <- c("Q1 2013","Q2 2013", "Q3 2013", "Q4 2013")
How can I be able to properly input this data into r as quarterly time series date so I would be able to perform analysis on it?
I tried to use yearqtr from zoo package but all I receive is NA.
as.Date(as.yearqtr(x, format = "Q%q /%yyyy"))
This could be because of the space between Q1 and 2013, I'm open to change my format if I have to but I'm not even sure what format would work in R. Should I change my columns to 1.2013, 2.2013, ... or this would also not be recognized as a date format by R? And how am I gonna be able to change them when I have a repeated sample of quarterly date in this format: Q1 2013, etc.
This should solve your problem:
as.yearqtr(format(x), "Q%q %Y")
This is the output:
# [1] "2013 Q1" "2013 Q2" "2013 Q3" "2013 Q4"
You can make it as dates meanwhile:
as.Date(as.yearqtr(format(x), "Q%q %Y"))
And the output would be:
# [1] "2013-01-01" "2013-04-01" "2013-07-01" "2013-10-01"
Related
Hello and thanks for being here.
I'm trying to convert dates with as.yearmon but the results I'm getting are odd and I do not know how to fix this; I tried searching on here and on the offical package guide without finding someone with the same problem.
The problem is that I transformed a column of a dataset which was formatted as "month/year" with as.yearmon but the results were not correct.
For example, the first 3 values of the column of the original DF are: "1/86", "2/86", "3/86".
After using this function to convert them:
library(zoo)
Dates <- Returns
Dates$Month<- zoo::as.yearmon(Dates$Month, "%m / %Y")
[Where "Returns" is the original dataframe and Dates the new one with the modified dates.]
The result I got, instead of being: "gen 1986", "feb 1986", "mar 1986" was "gen 0086", "feb 0086", "mar 0086" and I don know why.
[I should not that "gen", "feb", "mar" are in Italian; I do not know if that matters and I do not know how to change that to "Jan", "Feb", "Mar" which I think I'll have to do as well]
Thanks in advance for your help, if something is not clear just let me know; I'm still a rookie.
You need to use "%m/%y" instead of "%m/%Y". Your dates don't have a the full year notation.
x <- c("1/86", "2/86", "3/86")
zoo::as.yearmon(x, "%m/%y")
[1] "jan 1986" "feb 1986" "mrt 1986"
Date functions return the names in the local locale. If you want them in English:
Sys.setlocale("LC_TIME", "English")
zoo::as.yearmon(x, "%m/%y")
[1] "Jan 1986" "Feb 1986" "Mar 1986"
Everytime you restart R, this will be set back to your locale.
More info here on SO
I'm trying to convert a column to a date and am having some trouble. The dates are a in a column named month and each value is an abbreviated month followed by a year, like "Nov 2016" and "Mar 2017". What's the best way to convert this column to a date so I can use it as the x-axis on a graph.
Thanks!
This issue comes around a lot on Stack Overflow. Basically: month + year is not a date - you need a day too.
The usual solutions are (1) use zoo::as.yearmon to make a "year month" object:
library(zoo)
as.yearmon("Nov 2016", "%b %Y")
or (2) use the first of the month as an arbitrary day to create a date:
as.Date(paste("01", "Nov 2016"), "%d %b %Y")
I am working with a dateframe (INPUT) that contains number the of transaction of a product per calendar quarter. The first column (DATE) contains the calendar quarter in this format "2016 Q2". I would like to transform this date into the a financial quarter format such as "2016/17 Q1". The financial year start in the 1st April.
I came up with the following code which does the job, but I was wondering if there is a formula or a neater code that I could use.
INPUT$FY_Date=character(nrow(INPUT))
for (i in 1:nrow(INPUT)) {
INPUT$FY_Date[i]= if(substr(INPUT$DATE[i],7,7)==1) paste(as.numeric(substr(INPUT$DATE[i],1,4))-1,"/",substr(INPUT$DATE[i],3,4)," Q4",sep="") else
paste(substr(INPUT$DATE[i],1,4),"/", formatC(as.numeric(substr(INPUT$DATE[i],3,4))+1,width=2,format="d",flag=0)," Q",as.numeric(substr(INPUT$DATE[i],7,7))-1,sep="")
}
I could not find any previous related posts so I would appreciate any guidance.
Using the "yearqtr" class defined in zoo we can do it in two lines of code.
Convert to "yearqtr". The "yearqtr" class uses an internal representation of year + (qtr-1)/4 where qtr is 1, 2, 3 or 4 so adding 3/4 will shift it to the year-end year and fiscal quarter. Then in the final line of code as.integer will extract the year-end year. format function can be used to get the rest where %y means 2 digit year and %q means quarter.
library(zoo)
# test input
yq <- c("2016 Q2", "2016 Q3", "2016 Q4", "2017 Q1")
fyq <- as.yearqtr(yq, format = "%Y Q%q") + 3/4
paste0(as.integer(fyq) - 1, format(fyq, "/%y Q%q"))
giving:
[1] "2016/17 Q1" "2016/17 Q2" "2016/17 Q3" "2016/17 Q4"
Note that if you don't need the specific format shown in the question you could just use format(fyq) in place of the last line or maybe format(fyq, "%Y Q%q").
Update: Minor code improvements.
This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 5 years ago.
I need to convert following types of strings to a date format.
Convert "Feb 2009" to 2009-02-01
Convert "Jan 2010" to 2010-01-01
Convert "Mar 2011" to 2011-03-01
I can achieve this from the following code using zoo package.
as.Date(as.yearmon("Feb 2009"))
But due to some constraints I do not want to use this way of converting. So I want to know if there is any other way in R of achieving this task?
You can paste 01 to the vector using paste and then convert to date by specifying the appropriate format
as.Date(paste('01', v1), format='%d %b %Y')
#[1] "2009-02-01" "2010-01-01", "2011-03-01"
data
v1 <- c("Feb 2009", "Jan 2010", "Mar 2011")
I can generate quarterly OHLC date from a daily time series:
library(quantmod)
getSymbols("SPY", from="2000-01-01", to=Sys.Date())
tail(SPY)
dfQ <- to.quarterly(SPY[,6])
tail(dfQ)
I can also generate the quarterly mean:
dfmean1 <- apply.quarterly(xts(SPY[,6]), FUN = mean)
tail(dfmean1)
However I am having problems merging the two, with an index showing the first date of the quarter (rather than the last date of the quarter).
Thank you for your help
I think you have two questions here. The first is how to have a mean column in OHLC quarterly data. The second is how to have datestamps for the start of each quarter, instead of "last" datestamps. The xts/quantmod packages assume you want "last" datestamps, so go with the flow, and just replace the datestamps at the end.
To have mean with OHLC I've found it best just to do the OHLC calculation myself. So instead of passing mean to apply.quarterly(), do this:
bars = apply.quarterly(xts(SPY[,6]), FUN = function(x){
d=coredata(x);
c(first(d),max(d),min(d),last(d),mean(d))
} )
colnames(bars)=c("open","high","low","close","mean")
This gives:
...
2013-09-30 159.71 171.28 159.56 167.10 165.9822
2013-12-31 168.43 184.69 164.59 184.69 176.1416
2014-01-08 182.92 183.52 182.36 183.52 183.0340
Then to fix the datestamps:
index(bars) = as.Date(as.yearqtr(index(bars)))
To understand that, start by looking at index(bars), then look at as.yearqtr(index(bars)), which gives:
[1] "2000 Q1" "2000 Q2" "2000 Q3" ...
... "2013 Q3" "2013 Q4" "2014 Q1"
Then, as luck would have it, as.Date() gives you the datestamp of the start of each quarter.
The final bit is to assign the new index back to the bars object with index(bars) = ... (or index(bars) <- ... if you prefer).
By the way, there is also a indexAt="lastof" or indexAt="firstof" parameter you could give to to.quarterly(). Experiment with this, but in my tests it was not quite useful enough.