Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping over dates with R

Tags:

r

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?

like image 684
Bob Wakefield Avatar asked Sep 16 '15 13:09

Bob Wakefield


People also ask

Can you write a loop in R?

When you create a loop, R will execute the instructions in the loop a specified number of times or until a specified condition is met. There are three main types of loop in R: the for loop, the while loop and the repeat loop.

What is looping statement in R?

In R programming, we require a control structure to run a block of code multiple times. Loops come in the class of the most fundamental and strong programming concepts. A loop is a control statement that allows multiple executions of a statement or a set of statements. The word 'looping' means cycling or iterating.

How do you declare a date variable in R?

To create a Date object from a simple character string in R, you can use the as. Date() function. The character string has to obey a format that can be defined using a set of symbols (the examples correspond to 13 January, 1982): %Y : 4-digit year (1982)


1 Answers

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=""))
}
like image 81
Bob Wakefield Avatar answered Sep 18 '22 13:09

Bob Wakefield