用R循环日期

用R循环日期,r,R,我需要用R编写一些代码,通过循环日期来构建字符串,而我在书中或谷歌上似乎找不到这样的例子。基本上: for theDate = 1Jan14 to 31Dec14{ "http://website.com/api/" + theDate } 我曾考虑过创建一个保存日期的输入文件,但这似乎不雅观。有人知道更好的解决方案吗?您可以使用 > dates <- seq(as.Date("2014-01-01"), as.Date("2014-12-31"), by=1) @Rolan

我需要用R编写一些代码,通过循环日期来构建字符串,而我在书中或谷歌上似乎找不到这样的例子。基本上:

for theDate = 1Jan14 to 31Dec14{
 "http://website.com/api/" + theDate
} 
我曾考虑过创建一个保存日期的输入文件,但这似乎不雅观。有人知道更好的解决方案吗?

您可以使用

> dates <- seq(as.Date("2014-01-01"), as.Date("2014-12-31"), by=1)
@Roland的注释解决方案将为您提供以下形式的向量:

> paste0("http://website.com/api/", dates)
[1] "http://website.com/api/2014-01-01" "http://website.com/api/2014-01-02"
[3] "http://website.com/api/2014-01-03" "http://website.com/api/2014-01-04"
[5] "http://website.com/api/2014-01-05" "http://website.com/api/2014-01-06"
...

您可以将日期转换为朱利安日,然后根据朱利安日编写循环

要转换为julian days,可以使用下面描述的代码

然后,您可以使用朱利安时代编写代码,如:

tmp <- as.POSIXlt("1Jan14", format = "%d%b%y")
strdate <- julian(tmp)
tmp <- as.POSIXlt("31Dec14", format = "%d%b%y")
enddate <- julian(tmp)

for (theDate in strdate:enddate){
    paste ("http://website.com/api/", toString(theDate), sep = "")
}

tmp当然,在我问了这个问题之后,我碰巧发现了这个

days <- seq(from=as.Date('2011-02-01'), to=as.Date("2011-03-02"),by='days' )
for ( i in seq_along(days) )
{
  print(paste(days[i],"T12:00:00", sep=""))
}

days这不会消耗那么多内存,也不需要使用
julian
功能:

start <- as.Date("01-08-14",format="%d-%m-%y")
end   <- as.Date("08-09-14",format="%d-%m-%y")

theDate <- start

while (theDate <= end)
{
  print(paste0("http://website.com/api/",format(theDate,"%d%b%y")))
  theDate <- theDate + 1                    
}

paste0(你的URL,你的日期向量)
日期不是向量,这是问题所在。等等。好的,我知道你是怎么把日期转换成向量的了!运行代码时,我收到一个错误:“+二进制运算符的日期:非数字参数中出现错误。您需要使用paste或paste0将其附加到字符串中。很抱歉更改了它。这是Python的方式,顺便说一句,要简单得多。不确定内存消耗情况,但这比使用
paste0(“http://website.com/api/,格式(seq(开始、结束、结束日期=1),%d%b%y”)
。通常你不想在r中使用循环。我不相信这里的目标是打印。和
while
循环?拜托,这应该是R码。
start <- as.Date("01-08-14",format="%d-%m-%y")
end   <- as.Date("08-09-14",format="%d-%m-%y")

theDate <- start

while (theDate <= end)
{
  print(paste0("http://website.com/api/",format(theDate,"%d%b%y")))
  theDate <- theDate + 1                    
}
[1] "http://website.com/api/01Aug14"
[1] "http://website.com/api/02Aug14"
[1] "http://website.com/api/03Aug14"
[1] "http://website.com/api/04Aug14"
[1] "http://website.com/api/05Aug14"
[1] "http://website.com/api/06Aug14"
[1] "http://website.com/api/07Aug14"
[1] "http://website.com/api/08Aug14"
[1] "http://website.com/api/09Aug14"
[1] "http://website.com/api/10Aug14"
[1] "http://website.com/api/11Aug14"
[1] "http://website.com/api/12Aug14"
[1] "http://website.com/api/13Aug14"
[1] "http://website.com/api/14Aug14"
[1] "http://website.com/api/15Aug14"
[1] "http://website.com/api/16Aug14"
[1] "http://website.com/api/17Aug14"
[1] "http://website.com/api/18Aug14"
[1] "http://website.com/api/19Aug14"
[1] "http://website.com/api/20Aug14"
[1] "http://website.com/api/21Aug14"
[1] "http://website.com/api/22Aug14"
[1] "http://website.com/api/23Aug14"
[1] "http://website.com/api/24Aug14"
[1] "http://website.com/api/25Aug14"
[1] "http://website.com/api/26Aug14"
[1] "http://website.com/api/27Aug14"
[1] "http://website.com/api/28Aug14"
[1] "http://website.com/api/29Aug14"
[1] "http://website.com/api/30Aug14"
[1] "http://website.com/api/31Aug14"
[1] "http://website.com/api/01Sep14"
[1] "http://website.com/api/02Sep14"
[1] "http://website.com/api/03Sep14"
[1] "http://website.com/api/04Sep14"
[1] "http://website.com/api/05Sep14"
[1] "http://website.com/api/06Sep14"
[1] "http://website.com/api/07Sep14"
[1] "http://website.com/api/08Sep14"
>