R在6个月内选择以前的汽车所有权

时间:2016-08-26 10:03:59

标签: r

为清晰起见编辑:

我是R的新手。我有一个数据框,车辆上有多个所有权的开始和结束日期。我正在寻找在他们目前所有权的6个月内拥有之前所有权的车辆。

vhc_key     start_date  end_date
1           2006-12-16  2015-10-05
1           2015-11-21  NA
2           2014-01-14  2014-02-14
2           2014-03-14  NA
3           2014-01-14  2014-02-14
3           2015-03-14  NA

结果:

vhc_key     start_date  end_date
1           2006-12-16  2015-10-05
2           2014-01-14  2014-02-14

TRUE =车辆1和2,他们之前的结束日期是他们上次开始日期的6个月内。 FALSE =车辆3,其上一个end_date 2014-02-14不在2015-03-14最后开始日期的6个月内。

使用Dplyr它会是这样的,但这似乎不起作用:

df <- df %>%
  group_by(vhc_key) %>%
  filter(end_date >= max(start_date) - months(6))

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

这可能会有效,假设您的数据集已正确排序(就每辆车的开始日期而言)。

var data = [
  {
    "State": "CA",
    "Under 5 Years": 2704659,
    "5 to 13 Years": 4499890,
    "14 to 17 Years": 2159981,
    "18 to 24 Years": 3853788,
    "25 to 44 Years": 10604510,
    "45 to 64 Years": 8819342,
    "65 Years and Over": 4114496
  },
  {
    "State": "TX",
    "Under 5 Years": 2027307,
    "5 to 13 Years": 3277946,
    "14 to 17 Years": 1420518,
    "18 to 24 Years": 2454721,
    "25 to 44 Years": 7017731,
    "45 to 64 Years": 5656528,
    "65 Years and Over": 2472223
  },
  {
    "State": "NY",
    "Under 5 Years": 1208495,
    "5 to 13 Years": 2141490,
    "14 to 17 Years": 1058031,
    "18 to 24 Years": 1999120,
    "25 to 44 Years": 5355235,
    "45 to 64 Years": 5120254,
    "65 Years and Over": 2607672
  },
  {
    "State": "FL",
    "Under 5 Years": 1140516,
    "5 to 13 Years": 1938695,
    "14 to 17 Years": 925060,
    "18 to 24 Years": 1607297,
    "25 to 44 Years": 4782119,
    "45 to 64 Years": 4746856,
    "65 Years and Over": 3187797
  },
  {
    "State": "IL",
    "Under 5 Years": 894368,
    "5 to 13 Years": 1558919,
    "14 to 17 Years": 725973,
    "18 to 24 Years": 1311479,
    "25 to 44 Years": 3596343,
    "45 to 64 Years": 3239173,
    "65 Years and Over": 1575308
  },
  {
    "State": "PA",
    "Under 5 Years": 737462,
    "5 to 13 Years": 1345341,
    "14 to 17 Years": 679201,
    "18 to 24 Years": 1203944,
    "25 to 44 Years": 3157759,
    "45 to 64 Years": 3414001,
    "65 Years and Over": 1910571
  }
]