Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
循环asJSON()并在R中迭代日期_R_Json_Regex_Loops_Date - Fatal编程技术网

循环asJSON()并在R中迭代日期

循环asJSON()并在R中迭代日期,r,json,regex,loops,date,R,Json,Regex,Loops,Date,一个工作项目让我在两年多的时间里第一次推出了R 我有密码(为了隐私而修改) 我希望在该URL中迭代UTC时间码,以在每次迭代中获得24小时,并从中提取JSON以进入与日期对应的对象 我的问题是: 我怎样才能每天制作一件物品 我如何迭代URL中的日期以进行如下拉取: 20200101 <- fromJSON("...?date_from=202001010000Z&date_to=202001012359Z&limit=100") 20200102 <

一个工作项目让我在两年多的时间里第一次推出了R

我有密码(为了隐私而修改)

我希望在该URL中迭代UTC时间码,以在每次迭代中获得24小时,并从中提取JSON以进入与日期对应的对象

我的问题是:

我怎样才能每天制作一件物品

我如何迭代URL中的日期以进行如下拉取:

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

20200101您可以编写一个传递开始日期和结束日期的函数

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)
}
get_数据
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)
}
all_data <- get_data('20200101', '20200105')