按日期范围子设置不平衡面板数据

时间:2019-03-03 10:50:48

标签: r date

我有一个这样的不平衡面板数据集。

date          firms     return
2003-03-01      A       2
2003-04-01      A       5
2003-05-01      A       1
2003-06-01      A       4
2003-07-01      A       4
2003-08-01      A       3
2003-09-01      A       2
2003-10-01      A       5
2003-11-01      A       3
2003-12-01      A       2
2004-01-01      A       8
2004-02-01      A       4
2004-03-01      A       3
2004-04-01      A       5
2004-05-01      A       3
2004-06-01      A       2
2004-07-01      A       2
2004-08-01      A       5
2004-09-01      A       1
2004-10-01      A       4
2004-11-01      A       4
2004-12-01      A       3
2003-03-01      B       3
2003-04-01      B       5
2003-05-01      B       3
2003-06-01      B       2
2003-07-01      B       2
2003-08-01      B       5
2003-09-01      B       3
2003-10-01      B       2
2003-11-01      B       8
2003-12-01      B       4
2004-01-01      B       3
2004-02-01      B       3
2004-03-01      B       5
2004-04-01      B       3
2004-05-01      B       2
2004-06-01      B       2
2004-07-01      B       5
2004-08-01      B       1
2004-09-01      B       4
2004-10-01      B       4
2004-11-01      B       3
2004-12-01      B       3
2005-01-01      B       3
2005-02-01      B       3
2005-03-01      B       5
2005-04-01      B       3
2005-05-01      B       2
2005-06-01      B       2
2005-07-01      B       5
2005-08-01      B       3
2005-09-01      B       2
2005-10-01      B       8
2005-11-01      B       4
2005-12-01      B       4

数据是每月不平衡面板,其中所有公司的观察日期都不同。我想按日期将该集合分为两部分。我通过使用此代码进行了尝试,但无法正常工作

requre(data.table)
df1<-testset[date %between% c("2003-01-01", "2004-06-01")]
df2<-testset[date %between% c("2004-07-01", "2006-06-01")]

您能给我一些更好的代码吗,我可以通过它在我喜欢的任何日期范围内对其进行子集化?

1 个答案:

答案 0 :(得分:1)

假设您的数据具有此结构。

> str(testset)
'data.frame':   56 obs. of  3 variables:
 $ date  : Factor w/ 34 levels "2003-03-01","2003-04-01",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ firms : Factor w/ 2 levels "A","B": 1 1 1 1 1 1 1 1 1 1 ...
 $ return: int  2 5 1 4 4 3 2 5 3 2 ...

您可以将日期转换为POSIXct格式,以使代码运行。

testset$date <- as.POSIXct(testset$date)

library(data.table)
df1 <- testset[testset$date %between% c("2003-01-01", "2004-06-01"), ]
df2 <- testset[testset$date %between% c("2004-07-01", "2006-06-01"), ]