R:将 JSON 时间格式转换为 POSIX
我有一个 JSON 字符串,并将其放入数据框中。我能够做到这一点,但我在使用 apply 函数之一将所有时间字符串转换为 POSIX 格式时遇到问题。
请参阅此处了解更多背景信息。
JSON时间格式为:
%h-%m-%dT%H:%M:%S-
2012-01-29T17:00:45-11:00
可以说我有一个如图所示的数据框:
.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
要获取此数据我这样做了:
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))
我想将 JSON 时间格式更改为 POSIX,所以首先我将删除 T 分隔符,然后合并它们,然后应用到每个格式。
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 现在已变为 2012-01-29 21:00:45
问题在于 a=x$time[[1]][1] 和 b = x$Time [[1]][2]。这些太具体了,如果我想将它们应用到数据框,我只会返回所有这些的第一次设置。
关于如何正确编码的任何线索。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将
as.POSIXlt
与format
参数一起使用(有关详细信息,请参阅as.Date
)。好吧,我必须执行man strftime
查看日期规范(*nix 系统)):这完全忽略了
+08:00
和-07:00
(您当前的代码确实如此)也) - 这是你的意图吗?您可以使用
%z
作为偏移量,但它不需要冒号,即+0800
和-0700
。因此,我们首先必须去掉冒号:这会正确地将偏移量添加到时间中。
You can use
as.POSIXlt
with aformat
parameter (seeas.Date
for details. Well, I had to doman strftime
to see the date specifications (*nix system)):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:This properly adds the offset to the time.