Year column to time series [duplicate] - r

This question already has answers here:
Convert four digit year values to class Date
(5 answers)
Closed 5 years ago.
OK, this should be really simple but I'm not 'getting it.' I have a data frame with a column "Year" that I want to convert to a time series, but the format is tripping me up. How do I convert the "Year" value to a date, with the actual date being the end of each respective year (e.g. 2015 -> December 31st 2015)?
Year Production
1 1900 38400000
2 1901 43400000
3 1902 49000000
4 1903 44100000
5 1904 49800000
Goal is to get this to a time series data frame. (e.g. xts)
It is not quite the same as a previous question that converted a vector of years to dates. "Convert four digit year values to date type". Goal is to index the data by date, converting it to xts or similar object.
Edited:
This was the final solution:
df <- xts(x = df_original, order.by = as.Date(paste0(df_original[,1], "-12-31")))
whereby the "[,1]" indicates the first column of the original data frame.

If you want each full date to be 31 December, you could use paste along with as.Date to cast to a date:
df$date <- as.Date(paste0(df$Year, "-12-31"))

In addition to Tim Biegeleisen's answer, I will just add another way
df$final_date <- as.Date(ISOdate(df$Year, 12, 31))

Related

extracting month and year from object of POSIX Dates and Times Classes in r

I tried to extract month and year from an object of POSIXlt class, however, the returned month seemed to be one month earlier and the year did not seem correct either. My R code is as follows:
The POSIXlt stores month of the year using 0 to 11 (0 for Jan, 11 for Dec), and the year is counted as years since 1990.

When I try to make a new column in r for each date to indicate its quarter it only gives me one response [duplicate]

This question already has answers here:
Format date as Year/Quarter
(10 answers)
Closed 2 years ago.
In one of my columns I am given a date. I am trying to make a new column to indicate which quarter that date lies in so that way i can determine how many observations entered a certain venue in each quarter. End goal is to sumarize based off of quarter. Here is an example of the code I am using and an example of the output. as.Date(as.yearqtr(x, format ="%Y-%m-%d" )) . As you can see my problem is that It only returns 2019 q2, and i am confused why. This is my code PARTIES$QUARTER <- (as.yearqtr(PARTIES$opened, format = "%Y-%M-%d"))
It should be %m for (month) and not %M (which specifies the Minute as decimal number (00–59).)
library(zoo)
PARTIES$QUARTER <- as.yearqtr(PARTIES$opened, format = "%Y-%m-%d")

Why does R impute the 12th of the month when formatting a year as a Date [duplicate]

This question already has answers here:
Convert four digit year values to class Date
(5 answers)
Closed 5 years ago.
I note in R if one calls as.Date(as.character(2002:2013), format='%Y') the output is
[1] "2002-01-12" "2003-01-12" "2004-01-12" ...
I would like R to give me the first of the month instead. I could supply the whole date, paste(2002, '01', '01', sep='-'), but am curious why the year-only format imputes the 12th of the month and also to see other solutions.
Ah, just found my answer: The missing sections of the Date object (month/day) are imputed from today's date (System Date).

Time series with date format %Y-%m [duplicate]

This question already has answers here:
time series plot with x axis ticks in month-year format in R
(1 answer)
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 5 years ago.
I have a data with two columns of values and one column of date in %Y-%m format. I learnt to use xts to transform dataframe to time series data. However, the result doesn't output the date correctly. In the end, I need to plot cnt vs YrMon(ordered by time on x-axis) and total vs YrMon.
YrMon cnt total
1: 2016-08 14.42857 705.071429
2: 2016-09 13.21429 3.642857
3: 2017-04 13.28571 344.000000
4: 2016-07 14.21429 673.142857
.....
Any suggestion? Thank you.
Does adding a dummy day and then converting the column into date help? Like this
paste(data$YrMon,"-01",sep = "")
data$YrMon <- as.Date(data$YrMon)

How do i subset a zoo object based on values for a particular month [duplicate]

This question already has an answer here:
Subsetting winter (Dez, Jan, Feb) from daily time series (zoo)
(1 answer)
Closed 7 years ago.
My zoo object
I Would like to create a subset of values (these are discharge values) containing only December flow values.
Thank you!
We can extract the 'months' from the index with format, get a logical index by comparing with 'Dec', and use that to subset the zoo object.
z1[format(index(z1), '%b')=='Dec']
#1938-12-03 1938-12-10 1938-12-17 1938-12-24 1938-12-31
# 49 50 51 52 53
If we convert to xts object, .indexmon from the xts package can be also used. The .indexmon starts from 0, so December is 11.
library(xts)
z1[.indexmon(as.xts(z1))==11]
Other options from the comments are using grep on the index to get the numeric index and subset (from #Pierre Lafortune)
z1[grep("-12-",index(z1))]
Or with subset/month option (from # G. Grothendieck)
subset(z1, months(time(z1)) == "December")
data
library(zoo)
z1 <- zoo(1:100, order.by = seq(as.Date('1938-01-01'),
length.out=100, by = '1 week'))

Resources