R中计算距离下一个生日的天数的函数

发布于 2025-01-19 20:33:08 字数 513 浏览 11 评论 0原文

伙计们有人可以帮我完成这项任务吗?我正在尝试在 R 中编写一个函数,其中它需要“字符”中的出生日期并计算距下一个生日还有多少天。我不断收到同样的错误

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文