Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 每月的第一个星期二_R_Date - Fatal编程技术网

R 每月的第一个星期二

R 每月的第一个星期二,r,date,R,Date,我正在尝试编写一个函数,该函数将日期向量作为输入并返回日期向量,其中输出是与输入日期匹配的当月第一个星期二的日期 所以2012-11-19-->2012-11-06,等等 我在一次约会中取得了一些成功,但未能推广到vector案例。有人能帮忙吗 这就是我到目前为止所做的: firstTuesday <- function(tt){ ct <- as.POSIXct(tt) lt <- as.POSIXlt(tt) firstOf <- as.POSIXlt(c

我正在尝试编写一个函数,该函数将日期向量作为输入并返回日期向量,其中输出是与输入日期匹配的当月第一个星期二的日期

所以
2012-11-19
-->
2012-11-06
,等等

我在一次约会中取得了一些成功,但未能推广到vector案例。有人能帮忙吗

这就是我到目前为止所做的:

firstTuesday <- function(tt){
  ct <- as.POSIXct(tt)
  lt <- as.POSIXlt(tt)
  firstOf <- as.POSIXlt(ct - 60*60*24* (lt$mday - 1))
  if (firstOf$wday > 2) 
  {
    adjDays <- (9 - firstOf$wday)
    firstTues <- as.POSIXlt(as.POSIXct(firstOf) + 60*60*24*adjDays)
  }
  else {
    adjDays  <- (2 - firstOf$wday)
    firstTues <- as.POSIXlt(as.POSIXct(firstOf) + 60*60*24*adjDays)
  }
  return(firstTues)
}

这使用了lubridate,使得逻辑更简单。给定一个日期向量,第二个函数将返回一个字符向量,类似于您的输入。你可以根据自己的需要改变事情

library(lubridate)

getTuesday = function(x) {
    date = ymd(x)
    first = floor_date(date,"month")
    dow = sapply(seq(0,6),function(x) wday(first+days(x)))
    firstTuesday = first + days(which(dow==3)-1)
    return(firstTuesday)
}

getMultipleTuesdays = function(y) {
    tmp = lapply(y, getTuesday)
    tmp = lapply(tmp, as.character)
    return(unlist(tmp))
}
编辑

样本输入/输出

getMultipleTuesdays(c("2012-11-19","2012-11-19","2011-01-15"))
[1] "2012-11-06" "2012-11-06" "2011-01-04"

下面是一个使用基函数的简单解决方案:

firstDayOfMonth <- function(dates, day="Mon", abbreviate=TRUE) {
  # first 7 days of month
  s <- lapply(as.Date(format(dates,"%Y-%m-01")), seq, by="day", length.out=7)
  # first day of month
  d <- lapply(s, function(d) d[weekdays(d,abbreviate)==day])
  # unlist converts to atomic, so use do.call(c,...) instead
  do.call(c, d)
}

如果您使用的是时间和日期,我强烈推荐lubridate软件包。谢谢——也许我会的。我不愿意添加新的包依赖项,但这可能是值得的。此外,将其中的3(道指==3)更改为1-7将允许您选择一周中的任何一天,周日是第1天。+1/接受。谢谢你的例子确实解决了这个问题,并且是我自己(基本)解决方案的灵感来源。每当我看到使用字符串解析解决日期问题时,我都会吐出一点口水;)(但我还是投了你一票)@hadley:awww,太好了P它比使用
POSIXlt
+1/谢谢Joshua。这确实很有帮助。我不打算切换accept,因为40.7k比59大得多。
firstDayOfMonth <- function(dates, day="Mon", abbreviate=TRUE) {
  # first 7 days of month
  s <- lapply(as.Date(format(dates,"%Y-%m-01")), seq, by="day", length.out=7)
  # first day of month
  d <- lapply(s, function(d) d[weekdays(d,abbreviate)==day])
  # unlist converts to atomic, so use do.call(c,...) instead
  do.call(c, d)
}
R> d <- as.Date(c("2012-11-19","2012-11-19","2011-01-15"))
R> firstDayOfMonth(d, "Tuesday", FALSE)
[1] "2012-11-06" "2012-11-06" "2011-01-04"