TLDR; go to the answers below, I have provided what I have done to solve my problem.
I want to change unixtime in a read CSV file so that I can export this CSV file to matlab for a school project. I have actual unixtimes in that column that I want as dates (I have found questions on how to do it on stackoverflow but they are not working - what do I mean? The codes I have used below I found on stackoverflow, but I have no fundamental knowledge of R).
After this question I want to find OHLC of the data using another answer on stackoverflow. The problem is I cant get past the first stage.
These are imported data for price, time and volume.
X33287 X1331992243 X4.985 X5.72E.00
[1,] 33291 1331992243 4.985 1.0000
[2,] 33291 1331992243 4.988 0.3300
[3,] 33291 1331992243 4.990 1.0000
[4,] 33291 1331992243 4.993 2.7800
[5,] 33292 1331992243 4.998 13.5000
[6,] 33293 1331992243 4.999 0.2660
......
Where the first column is number, second is unixdate time, 3rd price, 4th volume.
To get this far I used this: import using test<-read.csv(##FILELOCATION) test=as.matrix(test) ##I made a matrix from experimentation, I guess it was ##anyway
Then using
as.POSIXct(time,origin="1970-01-01")
I can find the values of the actual date (yay!), but then I can't do anything with it.
Look
A=seq(1,10)*0
for (i in 1:10)
{
A[i]=as.vector(as.POSIXct(test[i,2],origin="1970-01-01"))
print(A[i])
}
[1] 1331992243
[1] 1331992243
[1] 1331992243
[1] 1331992243
[1] 1331992243
[1] 1331992243
[1] 1331992243
[1] 1331992281
It's supposed to be a date, not unixtime. Then when I as.POSIXct(A)
it gives me the dates like this
[1] "2012-03-17 11:20:43 NDT" "2012-03-17 11:20:43 NDT" "2012-03-17 11:20:43 NDT" "2012-03-17 11:20:43 NDT"
[5] "2012-03-17 11:20:43 NDT" "2012-03-17 11:20:43 NDT" "2012-03-17
I've tried
x=seq(1,length(time))*0
and also x=seq(1,40)*0 to set up a vector. for (i in 1:10) { x[i]<- as.POSIXct(time[i,2],origin="1970-01-01") print(x[i]) }
tells me i have incorrect dimensions.
also tried
A=seq(1,40)*0
for (i in 1:40)
{
A[i]=as.vector(as.POSIXct(test[i,2],origin="1970-01-01"))
print(A[i])
}
....again it just outputs Unix time, then when I type
A
....it just brings the list of numbers (num)
[1] 1331992243 1331992243 1331992243 1331992243 1331992243 1331992243 1331992243 1331992281 1331993630
[10] 1331993693 1331993752 1331993754 1331994303 1331994884 1331998567 1331999674 1331999973 1331999984
[19] 1332002200 1332002326 1332002740 133200275
also tried this I saw in another question...also didnt work.
p=test[1:40,2]
A=seq(1,10)*0
for (i in 1:10)
{
A[i]=head(as.POSIXct(as.numeric(as.character(p[i])),origin="1970-01-01"))
print(A[i])
}
EDIT: using R: Assigning POSIXct class to a data frame
I did this:
text=read.csv("d:/test.csv")
X33287 X1331992243 X4.985 X5.72E.00
1 33288 1331992243 4.985 1.0000
2 33289 1331992243 4.988 0.3300
3 33290 1331992243 4.990 1.0000
..............
then taking out extra column, after making it a matrix with ,
then remove the first useless column
textm=as.matix(text)
textx=textm[,1:3] #redundent oops
which gave me
X1331992243 X4.985 X5.72E.00
[1,] 1331992243 4.985 1.0000
[2,] 1331992243 4.988 0.3300
[3,] 1331992243 4.990 1.0000
..............
then this works
myxts <- xts(testx[,2:4], order.by=as.POSIXct(testx[,1], format='%m/%d/%y %H:%M'))
##then you have your answer
to.minutes(myxts)
myxts.Open myxts.High myxts.Low myxts.Close
2012-03-17 11:20:43 4.985 4.999 4.985 4.999
2012-03-17 11:21:21 4.999 4.999 4.999 4.999
2012-03-17 11:43:50 4.907 4.907 4.907 4.907
2012-03-17 11:44:53 4.999 4.999 4.999 4.999
Now I just have to do this with 1000000 rows, then export it to CSV which matlab can read and start some data analysis. I'd like to continue with R, but only have a few days to finish some lagging indicator checks and a simple ANN.