0

Guys could someone help me finish this task. I'm trying to write a function in R where it takes a date of birth in "character" and calculates how many days it is until the next birthday. I keep getting the same error

aftellen <- function(n) {
  if(class(n) == "character" | class(n) == "date") {
    day_birth <- as.Date(n)
    today <- Sys.Date() + 1
    totaldays <- length(seq(from = today, to = day_birth, by = "days"))
    print(totaldays)
  } else {
    print("Geef een geldige geboortedatum in")
  }
}


aftellen("2022-11-03")

the error:
Error in seq.int(0, to0 - from, by) : wrong sign in 'by' argument
  • 3
    The function is working on my machine without any error! – TarJae Apr 07 '22 at 20:42
  • and [other approaches to days between](https://stackoverflow.com/questions/67076764/count-the-number-of-days-between-two-dates-per), but as said, your code is working on mine also. What does `>Sys.Date()` report for you? And Welcome to Stackoverflow. – Chris Apr 07 '22 at 20:51
  • 1
    Your function could be simpler: `as.Date("2022-11-03") - Sys.Date()` returns "Time difference of 210 days". – dcarlson Apr 07 '22 at 20:58

0 Answers0