1

I've searched through many solutions here but can't get exactly what I'm looking for in this situation.

I pull a JSON file from an API and get as far as get it into a list. The data is basically a list of values for a day in 5 minute intervals. The values are in a list of length 288 (as there are 288 5 minute intervals in a day) and there is a startTime object that starts at date-00:00:00 and then another of intervalLength '00:05:00' but these are not lists.

> dput(jdf)
list(startTime = "2021-11-07T00:00:00.000+00:00", intervalLength = "00:05:00", 
    values = list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
        0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
        0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
        0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 7.66666666666667, 
        7.66666666666667, 7.66666666666667, 16L, 16L, 16L, 28.3333333333333, 
        28.3333333333333, 28.3333333333333, 25.3333333333333, 
        25.3333333333333, 25.3333333333333, 36.6666666666667, 
        36.6666666666667, 36.6666666666667, 48.3333333333333, 
        48.3333333333333, 48.3333333333333, 45.3333333333333, 
        45.3333333333333, 45.3333333333333, 56.3333333333333, 
        56.3333333333333, 56.3333333333333, 57.6666666666667, 
        57.6666666666667, 57.6666666666667, 54.6666666666667, 
        54.6666666666667, 54.6666666666667, 62.6666666666667, 
        62.6666666666667, 62.6666666666667, 73.3333333333333, 
        73.3333333333333, 73.3333333333333, 79.3333333333333, 
        79.3333333333333, 79.3333333333333, 81.6666666666667, 
        81.6666666666667, 81.6666666666667, 85L, 85L, 85L, 99L, 
        99L, 99L, 92.3333333333333, 92.3333333333333, 92.3333333333333, 
        110L, 110L, 110L, 117L, 117L, 117L, 119L, 119L, 119L, 
        126.333333333333, 126.333333333333, 126.333333333333, 
        123.666666666667, 123.666666666667, 123.666666666667, 
        123L, 123L, 123L, 122.333333333333, 122.333333333333, 
        122.333333333333, 118.333333333333, 118.333333333333, 
        118.333333333333, 99L, 99L, 99L, 89.6666666666667, 89.6666666666667, 
        89.6666666666667, 79.6666666666667, 79.6666666666667, 
        79.6666666666667, 86L, 86L, 86L, 91.3333333333333, 91.3333333333333, 
        91.3333333333333, 99.3333333333333, 99.3333333333333, 
        99.3333333333333, 120L, 120L, 120L, 114.666666666667, 
        114.666666666667, 114.666666666667, 119.666666666667, 
        119.666666666667, 119.666666666667, 112.333333333333, 
        112.333333333333, 112.333333333333, 90.3333333333333, 
        90.3333333333333, 90.3333333333333, 81.3333333333333, 
        81.3333333333333, 81.3333333333333, 67.6666666666667, 
        67.6666666666667, 67.6666666666667, 67.6666666666667, 
        67.6666666666667, 67.6666666666667, 66L, 66L, 66L, 75L, 
        75L, 75L, 82.6666666666667, 82.6666666666667, 82.6666666666667, 
        80L, 80L, 80L, 81.3333333333333, 81.3333333333333, 81.3333333333333, 
        76.6666666666667, 76.6666666666667, 76.6666666666667, 
        67.3333333333333, 67.3333333333333, 67.3333333333333, 
        65.6666666666667, 65.6666666666667, 65.6666666666667, 
        57.3333333333333, 57.3333333333333, 57.3333333333333, 
        71.3333333333333, 71.3333333333333, 71.3333333333333, 
        84.3333333333333, 84.3333333333333, 84.3333333333333, 
        91L, 91L, 91L, 100L, 100L, 100L, 109.666666666667, 109.666666666667, 
        109.666666666667, 105L, 105L, 105L, 117.666666666667, 
        117.666666666667, 117.666666666667, 118L, 118L, 118L, 
        110.333333333333, 110.333333333333, 110.333333333333, 
        105L, 105L, 105L, 89L, 89L, 89L, 84L, 84L, 84L, 62.3333333333333, 
        62.3333333333333, 62.3333333333333, 48.3333333333333, 
        48.3333333333333, 48.3333333333333, 32.3333333333333, 
        32.3333333333333, 32.3333333333333, 21.3333333333333, 
        21.3333333333333, 21.3333333333333, 18L, 18L, 18L, 17.6666666666667, 
        17.6666666666667, 17.6666666666667, 14.3333333333333, 
        14.3333333333333, 14.3333333333333, 8.66666666666667, 
        8.66666666666667, 8.66666666666667, 5L, 5L, 5L, 1.33333333333333, 
        1.33333333333333, 1.33333333333333, 0L, 0L, 0L, 0L, 0L, 
        0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
        0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L))

To this

> glimpse(exampledate)
Rows: 288
Columns: 2
$ Time   <dttm> 2021-11-13 00:00:00, 2021-11-13 00:05:00, 2021-11-13 00:10:00, 2021-11-13 00:15:00, 2021-11-13 00:20:00, 2021-11-13 …
$ values <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …

Thanks in advance for any help.

4
  • 4
    Hi Ben. It's not clear what a concrete answer would look like here without a concrete working example. The structure of a JSON can be quite idiosyncratic, so anyone venturing an answer would need your actual input data (not a screenshot of the structure of the input data). Could you post the actual list object please? If it is called myjson then you would type dput(myjson) in your r console and paste the result (it will be long) into a code block in your question. You can declare a code block with three backticks on their own line. Commented Nov 12, 2021 at 20:31
  • 1
    Said a little more directly, Ben: I'm not going to spend time trying to transcribe your picture-of-data into something usable. Additionally, while I have the luxury of being able to see it, not everybody does, and images-of-code/data make it nearly impossible for screen-readers to give a good representation of your data/question. Your goal should be to make it as easy for us as possible to help you. (See meta.stackoverflow.com/a/285557 (and xkcd.com/2116/).) Commented Nov 12, 2021 at 20:37
  • Thanks for the feedback both I will try and reshape the ask in the original post Commented Nov 12, 2021 at 21:00
  • Looking at your data it seems things are happening on a 15-min level, not sure if you want to reduce your dataset to that level? Commented Nov 12, 2021 at 22:38

1 Answer 1

2

Are you looking for something like:

library(tidyverse)
library(rrapply)
library(lubridate)

start <- as_datetime(jdf[[1]])
interval <- seconds(hms(jdf[[2]]))

jdf %>%
  rrapply(., how = 'melt') %>%
  slice(-c(1:2)) %>%
  mutate(Time = start,
         add = interval * (0:(n() - 1)),
         Time = Time + add) %>%
  select(-L1, -L2, -add)

The first 10 rows:

       value                Time
1          0 2021-11-07 00:00:00
2          0 2021-11-07 00:05:00
3          0 2021-11-07 00:10:00
4          0 2021-11-07 00:15:00
5          0 2021-11-07 00:20:00
6          0 2021-11-07 00:25:00
7          0 2021-11-07 00:30:00
8          0 2021-11-07 00:35:00
9          0 2021-11-07 00:40:00
10         0 2021-11-07 00:45:00
Sign up to request clarification or add additional context in comments.

1 Comment

amazing thank you so much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.