11

I need to write some code in R that builds a string by looping over dates and I cant' seem to find an example of this in my books or by Googling. Basically:

for theDate = 1Jan14 to 31Dec14{
 "http://website.com/api/" + theDate
} 

I thought about creating an input file that held the dates, but that seems inelegant.Does anybody know of a better solution?

Bob Wakefield
  • 3,739
  • 4
  • 20
  • 30

4 Answers4

15

This doesn't consume that much memory and doesn't need the julian function:

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"
> 
mra68
  • 2,960
  • 1
  • 10
  • 17
  • 1
    Not sure about memory consumption, but this is quite a bit slower (about five times) than using `paste0("http://website.com/api/", format(seq(start, end, by=1), "%d%b%y"))`. Usually you don't want to use loops in r. – DGKarlsson Sep 16 '15 at 16:15
  • 2
    I don't believe the goal here is printing. And a `while` loop? Come on, this is supposed to be R code. – Roland Sep 16 '15 at 17:37
9

You can use

> dates <- seq(as.Date("2014-01-01"), as.Date("2014-12-31"), by=1)

to generate a vector of consecutive days. What you want to do with this is not entirely clear from your pseudo-code, but you can iterate directly over the vector (which is generally not what you want in R)

> for (d in dates) {
    # Code goes here.
}

The comment-solution by @Roland will give you a vector of the form:

> 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"
...
DGKarlsson
  • 1,091
  • 12
  • 18
7

Of course after I ask the question I happen to find this.

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=""))
}
Bob Wakefield
  • 3,739
  • 4
  • 20
  • 30
0

You could translate your date into julian days and then write a loop based on the julian days.

To convert to julian days you can use the code described here

And then you could write code using the the julian days like:

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

you have to figure out how to convert back. I am not to sure about the julian function. maybe you should also have a look into "yday" of lubridate package.

Community
  • 1
  • 1
Generic Wevers
  • 570
  • 6
  • 10