R: converting JSON time format into POSIX

时光怂恿深爱的人放手 提交于 2019-12-08 01:03:51

问题


I have a JSON character string that I put into a data frame. I am able to do it, but I am having trouble using one of the apply functions to convert all the time character strings into POSIX format.

See here for more background on that.

The JSON time format is:

%h-%m-%dT%H:%M:%S-

2012-01-29T17:00:45-11:00

Lets say I have a data frame as shown:

    .Close    .High     .Low    .Open                      Time
1 5.517339 5.539509 5.404098 5.495318 2012-01-30T12:00:45+08:00
2 5.485943 5.521242 5.467357 5.467641 2012-01-30T11:00:45+08:00
str(x)
'data.frame':   2 obs. of  5 variables:
 $ .Close: num  5.52 5.49
 $ .High : num  5.54 5.52
 $ .Low  : num  5.4 5.47
 $ .Open : num  5.5 5.47
 $ Time  : Factor w/ 2 levels "2012-01-30T12:00:45+08:00",..: 1 2

To get this data I did:

y = getURI(url5)
y
"[{\"close\":5.51465512590582,\"highest\":5.58424835532979,\"lowest\":5.51349813464496,\"open\":5.53871134631156,\"start_time\":\"2012-01-30T13:00:45+08:00\"},{\"close\":5.55283232755149,\"highest\":5.58422873584898,\"lowest\":5.40409845894964,\"open\":5.49531753804068,\"start_time\":\"2012-01-30T12:00:45+08:00\"}]"
x = fromJSON(y)
x = do.call(rbind,lapply(x,as.data.frame))

I want to change the JSON time format into POSIX so first I will get rid of that T seperator, then merge them, and then apply to each.

jsontime = function ( data ) {
    x = data
    x$Time=as.character(x$Time)
    x$Time = strsplit(x$Time,split="T")
    a = x$Time[[1]][1]
    b = x$Time[[1]][2]
    x$Time = paste(a,b,sep=" ")
    x$Time=as.POSIXlt(x$Time,origin="1970-01-01",tz="GMT")
    return (x)
    }

2012-01-29T22:00:45-07:00 has now become 2012-01-29 21:00:45

The problem is with the a=x$time[[1]][1] and b = x$Time[[1]][2]. These are too specific and if I want to apply these to a data frame I will only return the first time set for all of them.

Any clue on how I can code this correctly.


回答1:


You can use as.POSIXlt with a format parameter (see as.Date for details. Well, I had to do man strftime to see the date specifications (*nix system)):

x$Time <- as.POSIXlt(x$Time, format="%Y-%m-%dT%H:%M:%S", 
                     origin="1970-01-01",tz="GMT")

This completely disregards the +08:00 and -07:00 though (which your current code does too) - is that what you intended?

You can use %z for the offset, but it expects no colon, ie +0800 and -0700. So we first have to strip that colon:

# replace [+-]hh:mm with [+-]hhmm for timezone offset
# i.e. 2012-01-30T12:00:45+08:00 -> 2012-01-30T12:00:45+0800
x$Time <- gsub('([-+][0-9]{2}):([0-9]{2})$','\\1\\2',x$Time)
# do as.POSIXlt with %z
x$Time <- as.POSIXlt(x$Time, format="%Y-%m-%dT%H:%M:%S%z", 
                     origin="1970-01-01",tz="GMT")

This properly adds the offset to the time.



来源:https://stackoverflow.com/questions/9059726/r-converting-json-time-format-into-posix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!