Default Axis Format of PosixCt - r

Is there a way I can change the default format for how POSIXct labels appear when using plot and when they are part of a dataframe (Date HH:MM instead of just HH:MM)?
I would be nice if I could do this without having to issue an axis command each time or converting the dataframe to an xts object.

Answer goes to Vincent Zoonekynd.
You can use format argument in plot function to output the data in "%Y-%m-%d %H:%M" format.
Please see the code below:
df <- data.frame(
ms = c(10485849612, 10477641600, 10561104000, 10562745600),
value = 1:4
)
df$posix_time <- as.POSIXct(df$ms, origin = "1582-10-14", tz = "GMT")
plot(df$posix_time, df$value, format = "%Y-%m-%d %H:%M")
Output:

Related

Convert a character column to dates in R

I am trying to convert a data column (x_date) that has this form "31.03.2013" (the class is "character") into Dates in the form of "2013-01-31"
I tried with the following codes:
as.Date(x_date, format = "%d-%m-%Y")
as.Date(x_date, format="%Y-%m-%d")
as.Date(x_date,format= "%Y-%m-%d", tryFormats = c("%Y-%m-%d", "%Y/%m/%d", "%d.%m.%Y"), optional=FALSE )
in all of the three cases the complete data column turns into "NA".
Then I tried this code:
format.Date(x_date, format="%Y-%m-%d")
and I get an error warning.
Can anybody help me to convert my column into the respective Dates?
Specify the format of the data instead of in tryFormats
as.Date(x_date, format = '%d.%m.%Y')

Trouble using foverlaps with POSIXct objects - maybe due to fractional seconds?

I have 2 data tables and I would like to find the rows that overlap using foverlaps. I think I am getting tripped up because some of the dates have fractional seconds.
library(data.table)
First create a data table of shift times
On <- as.POSIXct(c("2017-08-01 00:05:54", "2017-08-01 00:07:20", "2017-08-01 00:21:53"), format = "%Y-%m-%d %H:%M:%S", tz = "UTC")
Off <- as.POSIXct(c("2017-08-01 00:05:54", "2017-08-01 00:07:20", "2017-08-01 00:21:53"), format = "%Y-%m-%d %H:%M:%S", tz = "UTC")
shifts <- data.table(On, Off)
Now create a data table of observations times
The first bunch of observation times are from Matlab, so need to be converted to POSIXct first. These end up giving me fractional seconds
timestamp <- c(736908.0041, 736908.0051, 736908.009, 736908.012, 736908.0152)
Obs = data.table(SightingTime = as.POSIXct((timestamp-719529)*86400, origin = "1970-01-01", tz = "UTC"))
#add a variable for the "date type"
Obs$DateType = "Long"
Add a row to the data table that does not have fractional seconds (for the purpose of this example)
Obs <- rbind(Obs, data.table(SightingTime=as.POSIXct("2017-08-01 00:05:54", format = "%Y-%m-%d %H:%M:%S", tz = "UTC"), DateType = "Short"))
create point intervals so can use foverlaps
Obs[, SightingTime2 := SightingTime]
get ready for foverlaps
setkey(Obs, SightingTime, SightingTime2)
setkey(shifts, On, Off)
do the overlap join
Obs.ov <- foverlaps(shifts, Obs ,type="any",nomatch=0L)
This results in Obs.ov having a single row - the overlaps with the "Short" date format. Rows with the "Long" date format don't get included in the overlap. I would have expected that three rows would overlap (assuming that the fractional seconds would be rounded off, I would expect overlaps with the 00:05:54 and 00:21:53 "Long" timestamps as well.
I think this might be due to the fractional seconds in the dates I converted from Matlab, but I don't know how to get rid of the fractional bit. I did try using
attributes(Obs$SightingTime)$format <- "%Y-%m-%d %H:%M:%OS"
as well as including the "format" argument when the SightingTime variable was created from the "timestamp" variable early on. But have had no luck with either.
I did look here How to format fractional seconds in POSIXct in r, but can't quite figure out what change I need to make based on this.
I found what I needed here Remove seconds from time in R
I just needed to round off the seconds after creating the SightingTime variable, but before creating the "SightingTime2" variable.
Obs$SightingTime <- as.POSIXct(round(Obs$SightingTime, units="secs"))
Now when I do the overlaps, I get the 3 overlapping rows as expected.

Create date-time index in R xts

I want to create date-time index for my xts time series.
I am starting with a date in this format:
I cant share with you my entire dataset but basically what I am doing is:
data$date <- strptime(data$date,
format = "%Y%m%d %H:%M:%OS",
tz = "GMT")
and later
EURGBP.xts <- xts(EURGBP[,-1],
EURGBP$date,
tzone = "GMT")
When I do this in my date time index I have something like this (Index on the left, column I am modifying to create index is on the right:
Why do I have this strange X at the beginning? What may be the reason for that? Any ideas? What am I doing wrong?
EDIT:
My initial date format looks like this:
date
1 20170922 00:00:00.067
2 20170922 00:00:04.582

ggplot2: how to graph only part of a time series

I would like to use ggplot to graph portions of time series data. For example, say I only wanted to graph the last five dates of this data. Is there away to specify this in ggplot without subsetting the data ahead of time? I tried using xlim, but it didn't work.
date <- c("2016-03-24","2016-03-25","2016-03-26","2016-03-27","2016-03-28",
"2016-03-29","2016-03-30","2016-03-31","2016-04-01","2016-04-02")
Temp <- c(35,34,92,42,21,47,37,42,63,12)
df <- data.frame(date,Temp)
My attempt:
ggplot(df) + geom_line(aes(x=date,y=Temp)) + xlim("2016-03-29","2016-04-02")
My dates are formatted as POSIXct.
You have to enter the xlim values as as.Date or as.POSIXct(). Is this what you want?
df$date <- as.Date(df$date, format= "%Y-%m-%d", tz = "UTC")
ggplot(df) + geom_line(aes(x=date,y=Temp)) +
xlim(as.Date(c("2016-03-30", "2016-04-02"), tz = "UTC", format = "%Y-%m-%d") )
PS: Be aware that you will get the following warning:
Warning message:
Removed 5 rows containing missing values (geom_path)

How can I reformat a series of dates in a vector in R

I have vector of dates that i'm trying to convert with as date but i'm not getting the expected output, when I sapply with as.Date instead of getting a series of reformatted dates I get the names as dates and some odd value.
dates = c("20-Mar-2015", "25-Jun-2015", "23-Sep-2015", "22-Dec-2015")
sapply(dates, as.Date, format = "%d-%b-%Y")
20-Mar-2015 25-Jun-2015 23-Sep-2015 22-Dec-2015
16514 16611 16701 16791
I would like each of the values in the vector to be showing the new formated value. E.g. like what would happen if as.Date was shown applied to each element
as.Date("20-Mar-2015", format = "%d-%b-%Y")
[1] "2015-03-20"
You can directly use as.Date(dates, format = "%d-%b-%Y"). as.Date is vectorized, i.e. it can take a vector as input, not only a single entry.
In your case:
dates <- c("20-Mar-2015", "25-Jun-2015", "23-Sep-2015", "22-Dec-2015")
as.Date(dates, format = "%d-%b-%Y")
# [1] "2015-03-20" "2015-06-25" "2015-09-23" "2015-12-22"

Resources