我有一张桌子:
Date Value1 Value2 ...
09/01/2008
10/01/2008
11/01/2008
12/01/2008
01/01/2009
02/01/2009
03/01/2008
04/01/2009
05/01/2009
06/01/2009
07/01/2009
08/01/2008
我需要将下一个日期分组:2008年9月1日10/01/2008 11/01/2008等于“2008”,其余日期等于“2008.2009”
是否可以做类似的事情:
Date[Date>=09/01/2008 & Date<=11/01/2008] <- 2008
Date[Date>=12/01/2008 & Date<=08/01/2009] <- 2008.2009
输出应采用以下格式:
Date Value1 Value2 ...
2008
2008
2008
2008.2009
2008.2009
2008.2009
2008.2009
2008.2009
2008.2009
2008.2009
2008.2009
2008.2009
谢谢!
答案 0 :(得分:2)
dat <-read.table(text="Date
09/01/2008
10/01/2008
11/01/2008
12/01/2008
01/01/2009
02/01/2009
03/01/2009
04/01/2009
05/01/2009
06/01/2009
07/01/2009
08/01/2009",head=T)
dat$dt <- with( dat, as.Date(Date,format="%m/%d/%Y"))
with(dat, c("2008", "2008.2009", "NA")[ findInterval( dt,
c( as.Date("2008/09/01") ,
as.Date("2008/11/02") ,
as.Date("2009/08/02") )
)
] )
# [1] "2008" "2008" "2008" "2008.2009" "2008.2009" "2008.2009"
# [7] "2008.2009" "2008.2009" "2008.2009" "2008.2009" "2008.2009" "2008.2009"
您需要将某些日期提前1天,以便findInterval
可以遵守您对“&gt; =”和“&lt; =”
答案 1 :(得分:2)
如果数据集中的日期范围仅为2008/09/01至2009/08/01,并且只需要2年组,您可以尝试使用data.table
进行以下操作。
# Use @BondedDust toy data
library(data.table)
setDT(dat) # convert to data table
dat[, new_col := ifelse(dt %between% c("2008-09-01", "2008-11-01"),
"2008", "2008.2009")]
dat
# you get
Date dt new_col
1: 09/01/2008 2008-09-01 2008
2: 10/01/2008 2008-10-01 2008
3: 11/01/2008 2008-11-01 2008
4: 12/01/2008 2008-12-01 2008.2009
5: 01/01/2009 2009-01-01 2008.2009
6: 02/01/2009 2009-02-01 2008.2009
7: 03/01/2009 2009-03-01 2008.2009
8: 04/01/2009 2009-04-01 2008.2009
9: 05/01/2009 2009-05-01 2008.2009
10: 06/01/2009 2009-06-01 2008.2009
11: 07/01/2009 2009-07-01 2008.2009
12: 08/01/2009 2009-08-01 2008.2009