R:将 JSON 时间格式转换为 POSIX

发布于 2024-12-29 20:30:56 字数 1747 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

め七分饶幸 2025-01-05 20:30:56

您可以将 as.POSIXltformat 参数一起使用(有关详细信息,请参阅 as.Date)。好吧,我必须执行 man strftime 查看日期规范(*nix 系统)):

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

这完全忽略了 +08:00-07:00 (您当前的代码确实如此)也) - 这是你的意图吗?

您可以使用 %z 作为偏移量,但它不需要冒号,即 +0800-0700。因此,我们首先必须去掉冒号:

# 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")

这会正确地将偏移量添加到时间中。

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})

This properly adds the offset to the time.

,'\\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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文