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

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)

Related

how to convert date in multiple character format into one standerd date format [duplicate]

This question already has answers here:
How can I fix corrupted dates in R?
(1 answer)
R datetime series missing values
(3 answers)
Closed 4 months ago.
I have a date file which has date in following format:
2017-01-04 00:00:00 (Y-d-m)
2017-03-22 00:00:00 (Y-m-d)
06/04/2017 (d/m-Y)
When I import this into R it takes it as a character. I want to convert it into Date format,but when I convert it into date format it takes some NA values
I have tried this:
df_TEST$UTC_date <- as.Date(ifelse(grepl("-", df_TEST$UTC_date),
as.Date(df_TEST$UTC_date, format = c("%Y-%m-%d")),
as.Date(df_TEST$UTC_date, format = c("%d/%m/%Y"))), origin = "1970-01-02")
but it does 't make a distinguish between (Y-d-m) and (Y-m-d). How can I convert these dates into one date format in R?

Change dates to first day of the month in R [duplicate]

This question already has answers here:
Changing date format in R
(7 answers)
How to round floor_date() to arbitrary date?
(1 answer)
Change day of the month in a Date to first day (01)
(5 answers)
Closed 3 years ago.
How do I change my dates to the first day of the month...so 3/31/19 becomes 3/1/19, 2/28/19 becomes 2/1/19, and so on.
Thanks
One of the many ways to do it is to use lubridate package.
library(lubridate)
date <- dmy('11/02/2019')
day(date) <- 1
date

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).

Year column to time series [duplicate]

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))

R - Converting a modified numeric Julian Date to "%M%D%YYYY %H:%M:%S.000" [duplicate]

This question already has answers here:
Converting excel DateTime serial number to R DateTime
(5 answers)
Closed 8 years ago.
I have a very large data set from an instrument which measures depth of a water column, and attaches a date/time stamp to each measurement as a modified Julian Date. Below is a subset of the data. The column is in a data.frame (date) and contains numeric values.
head(date)
Date
41498.736111,
41498.736123,
41498.736134,
41498.736146,
41498.736157,
41498.736169,
When I transfer the first 2 values (i.e. 41498.736111 and 41498.736123) from the Date column to Excel and format the column to (MM:DD:YYYY hh:mm:ss.000), I get the correct value of 8/12/2013 17:39:59.990 and 8/12/2013 17:40:01.027. This tells me that the origin date for these Julian days is "1899-12-30". I would do this all in Excel, but my dataset is much too large and I will have to do this several times for the next few years, so I'd like to be able to do it all in R.
Is there any way to convert the entire column of these modified Julian Dates to the format "%M%D%Y %H:%M:OS3"?
Yeah, you can just add the number of seconds to the origin:
as.POSIXct('1899-12-30')+(date$Date*24*60*60)

Resources