Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7185431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:24:50+00:00 2026-05-28T18:24:50+00:00

I have a JSON character string that I put into a data frame. I

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:24:51+00:00Added an answer on May 28, 2026 at 6:24 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one JSON that is coming in a string format. I need to
I have json data as a string that is being passed to javascript. Before
I have a REST-service that returns JSON. But the JSON response returned with \
I have been trying to compress and store a json encoded string into mysql,
I have a JSON array with ActiveRecord objects. These objects can be reconstructed using
I have a JSON result that contains numerous records. I'd like to show the
I have a json property that might be null, and I'd prefer to keep
I have Converted a C# class in Json Object after it I stringify that
I have a servlet that outputs JSON. The output encoding for the servlet is
We have a .properties file that has some values persisted as JSON. I keep

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.