I'm trying to create a new XTS object for a set of intraday FX data. The initial dataframe is called "one_day_series" and looks like this:
pair id date_time bid ask mid_price
1 USDCAD 485194239 2009-08-03 08:00:00.451 1.07679 1.07699 1.07689
The command I use to create an XTS object is as follows:
my_xts <- xts(one_day_series[,6], as.POSIXct(strptime(one_day_series[,3], "%Y-%m-%d %H:%M:%S")))
I get an XTS object out of this however the time has been reset to 00:00:02, an example:
row.names V1
1 2009-08-03 00:00:02 1.07591
I just want the time to be created correctly from the dataframe so I would be grateful if someone could help me understand what is going wrong here ?
Related
I have loaded in time series data of creek depth and am needing to calculate total annual values (Oct-April) for each year. The following is what I have tried thus far:
depth <- read_csv("Depth.Water_Depth-_All_Instruments#GCNP_-_Robber's_Roost_Spring.EntireRecord .csv")
The following is a screenshot of the resulting data frame
enter image description here
These are my attempts at making the timestamp column (ISO_UTC) into a date class. Although, each attempt makes all values for ISO_UTC into N/A values instead of dates / times.
ymd_hm(depth$ISO_UTC)
depth$ISO_UTC<- as.Date(depth$ISO_UTC, format = "%Y-%m-%dT%H:%M")
depth$ISO_UTC<- as.POSIXct(depth$ISO_UTC, format = "%Y-%m-%d %H:%M", tz = "US/Pacific")
Please help me to put these data into usable datetime values.
Please see the above details.
Following is an example of the column of the data frame
time
00:01:47
01:05:45
17:10:00
20,551,697 more rows
The data type is character
The data is from Citi Bikes New York for the period January 2019 to December 2019
Note: the data that starts with JC is for New Jersey not New York City
Since the data is very large you can download just one months data to try the code:
The column time is not a part of the original data frame and was created by me using the following code:
ridedata_clean$time <- format(as.POSIXct(ridedata_clean$starttime),format = "%H:%M:%S")
Then I tried to change the format from chr to time or date time, using the follwoing code:
ridedata_clean$time <- format(ridedata_clean$time, format ="%H:%M")
But glimpse() still shows the data type as so I used strptime(), but it still did'nt change the format
ridedata_clean$time <- strptime(ridedata_clean$time, "%H:%M:%S")
I have tried to use the round function, but it didn't work:
round(ridedata_clean$time, units = "hours")
The I tried lubridate, but it didn't work, the code below maybe wrong since I am typing from memory as I was unbale to save this change:
round_date(ridedata_clean$time, "%H,%M,%S")
I am new to data analysis and this is my second project, please help.
I have got a data frame with different columns.
One column is called "TIMESTAMP". Click on the column, I see it is a character, down below you see the content:
TIMESTAMP Price
2003-06-20 09:19:00 5.25
2003-06-20 09:21:00 5.34
2003-06-20 09:22:00 5.43
2003-06-20 09:23:00 5.32
I'd like to convert the complete "TIMESTAMP"-column into as.POSIXct
The reason for this is, that I want to afterwards add the missing minutes in the column as you see from row 1 to 2 there is the timestamp missing with 09:20:00. I want to add the missing minutes for 09:00:00 to 17:30:00, of course with the correct date too.
Let's call the dataframe data.
I tried as.POSIXct(data$TIMESTAMP, format="%Y-%m-%d %H:%M:%S"), but I m unsure if it was succesfull, because the data in the dataframe didn't change.
Is there also a hint how to add the missing timestamps after getting the correct format?
Thanks for your help!
What you tried is correct as long as you assign the result back to the column of your data frame. This is what you should do:
> data$TIMESTAMP <- as.POSIXct(data$TIMESTAMP, format="%Y-%m-%d %H:%M:%S")
After that, the TIMESTAMP column will have the desired class:
> class(data$TIMESTAMP)
[1] "POSIXct" "POSIXt"
For completing your data frame with missing lines, you can first build a new data.frame with all the expected times and then merge it to your initial data. Bellow I'm using min and max to find the range of date-time, then I'm using seq.POSIXt by minute to generate the full set of date-time. The merge will then use the already existing price values from your initial data frame:
> data_full <- data.frame(TIMESTAMP = seq.POSIXt(from=min(data$TIMESTAMP), to=max(data$TIMESTAMP), by='min'))
> data_complete <- merge(data_full, data, all.x = T)
I have a dataset consisting of 4 variables namely date,Gold price,crude price and dollar price. I converted the class of the data to time series using ts() function. After converting, the dates got changed into some value. I am able to retrieve the dates from converted values using as.Date() function.Now I want to replace the date values by date itself in the ts object's date variable.
#CONVERTING DATA FRAME TO TIME SERIES OBJECT.
Gold.ts <- ts(Gold,start=Gold$DATE[1])
head(Gold.ts)
#OUTPUT.
DATE GOLD.PRICE CRUDE DOLLAR.INR
[1,] 13152 533.9 63.42 44.705
[2,] 13153 526.3 62.79 44.600
[3,] 13154 539.7 64.21 44.320
head(as.Date(index(Gold.ts)))
[1] "2006-01-04" "2006-01-05" "2006-01-06" "2006-01-07" "2006-01-08" "2006- 01-09"
Gold.ts$DATE <- as.Date(index(Gold.ts)) # This won't work because $ is not acceptable to extract variables from a time series object.
index(Gold.ts) <- as.Date(index(Gold.ts)) #This should work but gives error. How to display date instead of values in time series object i.e Gold.ts?
What is the right way to do it?
Here we go againi, I am still banging my head against the wall on the above problem.
I have a data.frame that I upload via csv which looks like:
X SPY VTI
01.02.2002 0.0000 0.0000
04.02.2002 -2.4578 -2.4167
.....
31.12.2015 -1.003 -0.9685
where X is date and SPY and VTI are stock returns
I tried many things to convert to a time series. first I tried
spyvti$X <- as.Date(as.character(spyvti$X),format="%d.%m.%Y.")
and what I get is:
X SPY VTI
NA 0.0000 0.0000
NA -2.4856 -2.4167
.....
NA -1.003 -0.9685
so it looks like it can't convert the first column, which is a factor, in an object of class(Date).
I tried also to detach the data.drame into 3 different vectors, converting first the date vector into character, which worked, then
date <- as.Date(date, format = "%d.%m.%Y.")
error in charToDate(x):
character string is not in a standard unambiguous format.
So I'd like to get some help with overcoming the Date problem, and I'd like to know if, when the date problem is over, creating a ts object as below is correct
tsobject <- xts(date,spy)
where spy is a numeric.
Thanks a lot
Paolo
Use the "lubridate" package. It makes conversion of dates super easy.
library(lubridate)
dmy(spyvti$x)
I am making this up from my mind. Hope it works. You can try the following:
Yourdataframe$X<-strptime(as.character(Yourdataframe$X),format="%d.%m.%Y")
Yourdataframe<-xts(Yourdataframe[,2:3],order.by=Yourdataframe[,1]
Assuming your example data frame is named df you can convert it into a xts time series object like so:
library(xts)
xtsObject <- as.xts(df[,-1],order.by = as.Date(as.character(df[,1]), format = "%d.%m.%Y"))