How do I express an ISO8601 date as Day-Month? - r

I'm plotting time series data that has an ISO8601 variable as the x-axis; however, having the labels as ISO8601 isn't very readable. What is the best way to retain the ISO8601 variable but display the labels as Day Month (i.e. 28 Jun)?
Here is what I currently have:
format(as.Date("2020-06-28"),"%d %b"))
But that means they now display in order like:
c("03 May","04 Jun", "06 May","07 Jun")
How can I get them to arrange in Date order?

Related

Plotting date vs time using plotly in Jupyter-notebook

I have a data set whose format is given below
Date
Time
2-May-22
02:42:33 AM
3-May-22
01:05:41 AM
7-May-22
03:03:29 AM
Now I am plotting a line considering Date as x-axis and Time as y-axis
But when I'm using Time as string it is not being plotted as it should be. When I'm trying to convert it into datetime.time while plotting a random date (Jan 1,1900) is also being combined with it. What should I do.
For converting string to datetime.time I'm using this code:
time = datetime.datetime.strptime(datetime_string , "%H:%M:%S")

Formatting year month variable as date

In Stata I have a variable yearmonth which is formatted as 201201, 201202 etc. for the years 2012 - 2019, monthly with no gaps. When I format the variable as
format yearmonth %tm
The results look like: 2.0e+05 for all periods, with the exact same number each time. A Dickey-Fuller test tells me I have gaps in my data (I don't) and a tsfill command generates dozens of empty observations between each period.
How do I properly format my yearmonth variable so I can set it as a monthly date?
You do have gaps — between 201212 and 201301, for example. Consider a statement like
gen wanted = ym(floor(yearmonth/100), mod(yearmonth, 100))
which parses your integers like 201201 into year and month components. So floor(201201/100) is floor(2012.01) and so 2012 while mod(201201, 100) is 1. The two components are then the arguments of ym() which expects a year and a month argument.
Then and only then will your format statement do you want. That command won’t create date variables.
See help datetime in Stata for more information and Problem with displaying reformatted string into a four-digit year in Stata 17 for an explanation of the difference between a date value and a date display format.

R strftime - timezone issue & issue with empty cells in date colume

I am working on a shiny app, the dataframe to be loaded into the shiny app have two timestamp columns, open time and close time, all the rows in column have timestamps, but in close time column have several missing timestamps.
I am trying to convert these timestamps which are in dd mmm,yyyy hh:mm:ss format to yyyy-mm-dd hh:mm:ss format, the code that I am trying to use is
df1$OPEN.TIME <- strptime(df1$OPEN.TIME,format ='%d %b,%Y %H:%M:%S')
df1$CLOSED.TIME <- strptime(df1$CLOSED.TIME,format ='%d %b,%Y %H:%M:%S')
For the OPEN.TIME I am getting an output like this eg. (2016-01-12T14:24:34Z) but the time is different from the actual time on the column, I understand this is because strptimeis converting to timestamp to system timezone, on the next column CLOSED.TIME with empty, I get an error:
character string is not in a standard unambiguous format
here is an example dataframe that I am trying to work
df1<-data.table(Quiz = c(1:5),
OPEN.TIME=c("06 Jul,2017 08:00:23", "12 Jan,2016 09:24:34","17 Jan,2016 15:03:24", "15 Feb,2016 09:09:28", "08 Mar,2016 11:29:06"),
CLOSED.TIME=c("08 Mar,2016 11:29:06", "","02 Mar,2016 14:46:19", "13 Apr,2016 16:36:11", ""))

SAS to R datetime conversion

SAS documentation states the following for data and datetime values:
SAS time value: is a value representing the number of seconds since midnight of the current day. SAS time values are between 0 and 86400.
SAS datetime value: is a value representing the number of seconds between January 1, 1960 and an hour/minute/second within a specified date.
I'm willing to convert the following date and hour values with R, I have a big doubt for the hour (datetime) conversion, which one of the "HH:MM:SS" values within R_hour1 and R_hour2 is correct ?
I have to separate columns, SAS date = 20562 and SAS hour = 143659, in my table
R: R_date <- as.Date(as.integer(20562), origin="1960-01-01"); R_date
[1] "2016-04-18"
R: R_hour1 <- as.POSIXct(143659, origin = R_date); R_hour1
[1] "2016-04-19 17:54:19 CEST"
R: R_hour2 <- as.POSIXct(143659, origin = "1960-01-01"); R_hour2
[1] "1960-01-02 16:54:19 CET"
Similar to R, SAS Date and DateTime values can have whatever origin you wish them to. The default formats have a default (1/1/1960 for both), but you can use the datetime field to mean any origin you wish, and it will generally still work perfectly well with any of the datetime functions (though it will not display properly unless you write a custom format). It is very possible to have a different origin, as you show above with R_hour1.
As such, you would have to ask the person who generated the data what the meaning of the field is and what its origin should be.

Convert from dd/mm/yyyy to dd/mm in r

I have data spread over a period of two months. When I graph data points for each day, dates (dd/mm/yyyy) are overlapping and it is not possible to make sense of which date a certain point refers to. I tried to remove years from the date as they are not useful for the info I have and the dd/mm should leave enough space.
df$date<-as.Date(df$date, format="%d/%m")
However, it transforms the 01/09/2014 to 2015-09-01. I read that when the year is missing as.Date assumes current year and inputs it. Can I avoid this automatic insertion somehow?
something like this?
date <- as.Date("01/09/2014", format = %d/%m/%Y)
format(date, "%d/%m")
"01/09"

Resources