如何抽样同一个工作日的日期?

时间:2016-12-29 14:31:31

标签: r date time weekday

考虑日期样本:

t_s<-seq(as.POSIXct("2010-01-01 00:00:00"), as.POSIXct("2010-12-31 23:00:00"), by = '1 day')

和日期向量:

t<-seq(as.POSIXct("2010-02-01 00:00:00"), as.POSIXct("2010-2-10 23:00:00"), by = '1 day')

现在,我想在t_s中使用t中元素的相同工作日随机抽样日期。每个t元素的样本大小应为4。 例如,第一个元素"2010-02-01"的样本可能是"2010-06-28" "2010-5-31" "2010-8-02" "2010-10-04",因为它们都是星期一。

t_s的采样日期可能是多种,因为t中的某些日期共享同一工作日。但是,如果t_s远小于t(不是在示例情况下),则无法满足样本的唯一性。因此,需要包含和不包含多个t_s日期的样本方法。

我怎样才能获得这些样品?

1 个答案:

答案 0 :(得分:1)

这样的事情应该做你需要的事情

无需替换

{
  "project": {
    "version": "1.0.0-beta.22-1",
    "name": "floorbook"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.json",
      "prefix": "fb",
      "mobile": false,
      "styles": [
        "styles.css"
      ],
      "scripts": [],
      "environments": {
        "source": "environments/environment.ts",
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "addons": [],
  "packages": [],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "prefixInterfaces": false,
    "inline": {
      "style": false,
      "template": false
    },
    "spec": {
      "class": false,
      "component": true,
      "directive": true,
      "module": false,
      "pipe": true,
      "service": true
    }
  }
}

有替换

lapply(seq_along(t), function(x){
  sample(t_s[weekdays(t_s)==weekdays(t[x])], 4, replace = FALSE)
})

这些返回包含样本的列表

排除先前选择的日期

lapply(seq_along(t), function(x){
  sample(t_s[weekdays(t_s)==weekdays(t[x])], 4, replace = TRUE)
})