检查日期是否在R的间隔内 [英] Check if a date is within an interval in R

查看:87
本文介绍了检查日期是否在R的间隔内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了以下三个间隔:

I have these three intervals defined:

YEAR_1  <- interval(ymd('2002-09-01'), ymd('2003-08-31'))
YEAR_2  <- interval(ymd('2003-09-01'), ymd('2004-08-31')) 
YEAR_3  <- interval(ymd('2004-09-01'), ymd('2005-08-31'))

(在现实生活中,我有50个)

(in real life, I have 50 of these)

我有一个数据框(称为df),其中的一列充满了lubridate格式的日期.

I have a dataframe (called df) with a column full of lubridate formatted dates.

我想在df上添加一个新列,该列具有适当的值YEAR_n,具体取决于日期的间隔时间.

I'd like to append a new column on df which has the appropriate value YEAR_n, depending on which interval the date falls within.

类似:

df$YR <- ifelse(df$DATE %within% YEAR_1, 1, NA)

但是我不确定如何继续.我需要以某种方式使用我认为的apply?

but I'm not sure how to proceed. I need to somehow use an apply I think?

这是我的数据框:

structure(c(1055289600, 1092182400, 1086220800, 1074556800, 1109289600, 
1041897600, 1069200000, 1047427200, 1072656000, 1048636800, 1092873600, 
1090195200, 1051574400, 1052179200, 1130371200, 1242777600, 1140652800, 
1137974400, 1045526400, 1111104000, 1073952000, 1052870400, 1087948800, 
1053993600, 1039564800, 1141603200, 1074038400, 1105315200, 1060560000, 
1072051200, 1046217600, 1107129600, 1088553600, 1071619200, 1115596800, 
1050364800, 1147046400, 1083628800, 1056412800, 1159747200, 1087257600, 
1201478400, 1120521600, 1066176000, 1034553600, 1057622400, 1078876800, 
1010880000, 1133913600, 1098230400, 1170806400, 1037318400, 1070409600, 
1091577600, 1057708800, 1182556800, 1091059200, 1058227200, 1061337600, 
1034121600, 1067644800, 1039478400, 1022198400, 1063065600, 1096329600, 
1049760000, 1081728000, 1016150400, 1029801600, 1059350400, 1087257600, 
1181692800, 1310947200, 1125446400, 1057104000, NA, 1085529600, 
1037664000, 1091577600, 1080518400, 1110758400, 1092787200, 1094601600, 
1169424000, 1232582400, 1058918400, 1021420800, 1133136000, 1030320000, 
1060732800, 1035244800, 1090800000, 1129161600, 1055808000, 1060646400, 
1028678400, 1075852800, 1144627200, 1111363200, 1070236800), class = c("POSIXct", 
"POSIXt"), tzone = "UTC")

推荐答案

每个人都有自己喜欢的工具,而我的恰好是 data.table ,因为它被称为dt[i, j, by]逻辑.

Everybody has their favourite tool for this, mine happens to be data.table because of what it refers to as its dt[i, j, by] logic.

library(data.table)

dt <- data.table(date = as.IDate(pt))

dt[, YR := 0.0 ]                        # I am using a numeric for year here...

dt[ date >= as.IDate("2002-09-01") & date <= as.IDate("2003-08-31"), YR := 1 ]
dt[ date >= as.IDate("2003-09-01") & date <= as.IDate("2004-08-31"), YR := 2 ]
dt[ date >= as.IDate("2004-09-01") & date <= as.IDate("2005-08-31"), YR := 3 ]

我创建了一个data.table对象,将您的时间转换为日期,以便以后进行比较.然后,我设置了一个新列,默认为一列.

I create a data.table object, converting your times to date for later comparison. I then set up a new column, defaulting to one.

然后我们执行三个条件语句:对于三个间隔中的每个间隔(我只是使用端点手动创建),我们将YR值设置为1、2或3.

We then execute three conditional statements: for each of the three intervals (which I just create by hand using the endpoints), we set the YR value to 1, 2 or 3.

正如我们从中看到的那样,这确实具有预期的效果

This does have the desired effect as we can see from

R> print(dt, topn=5, nrows=10)
           date YR
  1: 2003-06-11  1
  2: 2004-08-11  2
  3: 2004-06-03  2
  4: 2004-01-20  2
  5: 2005-02-25  3
 ---              
 96: 2002-08-07  0
 97: 2004-02-04  2
 98: 2006-04-10  0
 99: 2005-03-21  3
100: 2003-12-01  2
R> table(dt[, YR])

 0  1  2  3 
26 31 31 12 
R> 

一个人也可以简单地通过计算日期差并向下截断来做到这一点,但有时也要明确一点也是很好的.

One could have done this also simply by computing date differences and truncating down, but it is also nice to be a little explicit at times.

一个更通用的形式只是在日期上使用算术:

A more generic form just uses arithmetic on the dates:

R> dt[, YR2 := trunc(as.numeric(difftime(as.Date(date), 
+                                        as.Date("2001-09-01"),
+                                        unit="days"))/365.25)]
R> table(dt[, YR2])

 0  1  2  3  4  5  6  7  9 
 7 31 31 12  9  5  1  2  1 
R> 

这一步完成了工作.

这篇关于检查日期是否在R的间隔内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆