0

A work project has me bringing out R for the first time in over 2 years.

I have the code (redacted for privacy)

fromJSON("fake-api.com/test?date_from=202001010000Z&date_to=202001012359Z&limit=100")

I would like to iterate the UTC timecode inside that URL to gain 24 hours with each iteration, and have the JSON pulled from it to go into an object corresponding with the date.

My questions are:

How can I make an object for each day?

How can I iterate the dates in the URL to pull like this:

20200101 <- fromJSON("...?date_from=202001010000Z&date_to=202001012359Z&limit=100")
20200102 <- fromJSON("...?date_from=202001020000Z&date_to=202001022359Z&limit=100")
20200103 <- fromJSON("...?date_from=202001030000Z&date_to=202001032359Z&limit=100")
20200104 <- fromJSON("...?date_from=202001040000Z&date_to=202001042359Z&limit=100")
20200105 <- fromJSON("...?date_from=202001050000Z&date_to=202001052359Z&limit=100")

I am assuming it's a For Loop and some Regex, the former I have forgotten how to do, and the latter I never really knew.

Thank you for your help and advice!

Benjamin

zabada
  • 67
  • 1
  • 10

1 Answers1

1

You can write a function passing the start date and end date.

get_data <- function(start_date, end_date) {
   #Convert start and end date to Date object
   start_date <- as.Date(start_date, '%Y%m%d')
   end_date <- as.Date(end_date, '%Y%m%d')
   #Create a sequence of days from start to end date 
   #and get the data in original format
   dates <- format(seq(start_date, end_date, by = '1 day'), '%Y%m%d')
   #Create the URL to extract
   all_url <- sprintf('fake-api.com/test?date_from=%s0000Z&date_to=%s2359Z&limit=100', dates, dates)
   #Use lapply to read the data from each url
   all_data <- lapply(all_url, jsonlite::fromJSON)
   #Return list of data read
   return(all_data)
}

and call it as :

all_data <- get_data('20200101', '20200105')

all_data will be a list where each element would be a data for each day which can be accessed by all_data[[1]], all_data[[2]] etc.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213