-1

I want to determine the number of days between each of a list of dates and a specified date - here 1987-01-01. Looking at other examples and using some example data I write:

Example<-as.Date(c("1987-04-01", "1987-04-02", "1987-04-03", "1987-04-04", "1987-04-05"))
DayNumb<-for (i in Example) { 
             difftime("i", "1987-01-01")}

or

DayNumb<-for (i in Example) {
          difftime(as.Date("i"), ("1987-01-01"))}

but am told in both cases there is an error caused by

character string is not in a standard unambiguous format

What am I doing wrong?

1

This seems not to be related to GIS or spatial analysis and should have been researched/asked at Stack Overflow as a general R question. In future make sure that you are posting to the correct Stack Exchange site. This is a simple issue so, I am answering it here but, am also flagging it as inappropriate for GIS Stack Exchange.

There are a few issues with your code. First, your iterator is being called incorrectly; second, you are attempting to append the results by pipeing from the for loop, which will not work; and third you are not coercing the "difftime" object to a vector. This code should work.

example <- as.Date(c("1987-04-01", "1987-04-02", "1987-04-03", "1987-04-04", "1987-04-05"))
day.num <- vector()
  for(i in 1:length(example)) { 
    day.num <- append(day.num, as.vector(difftime(example[i], "1987-01-01"))) 
  }
round(day.num,0)

Not the answer you're looking for? Browse other questions tagged or ask your own question.