查找特定日期所属的季节 [英] Find which season a particular date belongs to

查看:136
本文介绍了查找特定日期所属的季节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期的向量,对于每个条目,我想分配一个季节。例如,如果日期在21.12之间。和21.3。我会说这是 winter 。到目前为止,我已经尝试了以下代码,但是我无法使它更通用,不管年份如何。

  my.dates<  -  as.Date(2011-12-01,format =%Y-%m-%d)+ 0:60 
low.date< - as .Date(2011-12-15,format =%Y-%m-%d)
high.date< - as.Date(2012-01-15,format =% Y-%m-%d)

my.dates [my.dates< = high.date& my.dates> = low.date]
[1]2011-12-152011-12-162011-12-172011-12-182011-12- 192011-12-202011-12-212011-12-222011-12-232011-12-242011-12-25
[12 ]2011-12-262011-12-272011-12-282011-12-292011-12-302011-12-312012-01-01 2012-01-022012-01-032012-01-042012-01-05
[23]2012-01-062012-01-07 2012-01-082012-01-092012-01-102012-01-112012-01-122012-01-132012-01-142012 -01-15

我已经尝试格式化日期,但没有一年,但它不工作。

  ld<  -  as.Date(12-15,format =%m-%d)
hd< - as.Date(01-15,format =%m-%d)
my.dates [my.dates< = hd& my.dates> = ld]


解决方案

这样做:

  getSeason<  -  function(DATES){
WS< - as.Date 2012-12-15,format =%Y-%m-%d)#冬至
SE< - as.Date(2012-3-15,format =%Y-% m-%d)#Spring Equinox
SS< - as.Date(2012-6-15,format =%Y-%m-%d)#夏至
FE < - as.Date(2012-9-15,format =%Y-%m-%d)#秋季Equinox

#将日期从任何一年到2012年日期
d< - as.Date(strftime(DATES,format =2012-%m-%d))

ifelse(d> = WS | d ifelse(d> = SE& d< SS,Spring,
ifelse(d> = SS& d< FE,Summer,Fall)) )
}

my.dates< - as.Date(2011-12-01,format =%Y-%m-%d)+ 0:60
head(getSeason(my.dates),24)
#[1]FallFallFallFallFallFallFall
#秋天秋天秋天秋天秋天秋天
#[15]冬天冬季冬季冬季冬季冬季

一个说明: 2012年是一个美好的一年,的日期;由于这是一个闰年,您的数据集中的任何2月29日将顺利进行处理。


I have a vector of dates and for each entry, I would like to assign a season. So for example, if a date is between 21.12. and 21.3., I would says that's winter. So far I have tried the following code but I couldn't make it more generic, irrespective of the year.

my.dates <- as.Date("2011-12-01", format = "%Y-%m-%d") + 0:60
low.date <- as.Date("2011-12-15", format = "%Y-%m-%d")
high.date <- as.Date("2012-01-15", format = "%Y-%m-%d")

my.dates[my.dates <= high.date & my.dates >= low.date] 
 [1] "2011-12-15" "2011-12-16" "2011-12-17" "2011-12-18" "2011-12-19" "2011-12-20" "2011-12-21" "2011-12-22" "2011-12-23" "2011-12-24" "2011-12-25"
[12] "2011-12-26" "2011-12-27" "2011-12-28" "2011-12-29" "2011-12-30" "2011-12-31" "2012-01-01" "2012-01-02" "2012-01-03" "2012-01-04" "2012-01-05"
[23] "2012-01-06" "2012-01-07" "2012-01-08" "2012-01-09" "2012-01-10" "2012-01-11" "2012-01-12" "2012-01-13" "2012-01-14" "2012-01-15"

I have tried formatting the dates without the year, but it isn't working.

ld <- as.Date("12-15", format = "%m-%d")
hd <- as.Date("01-15", format = "%m-%d")
my.dates[my.dates <= hd & my.dates >= ld] 

解决方案

How about using something like this:

getSeason <- function(DATES) {
    WS <- as.Date("2012-12-15", format = "%Y-%m-%d") # Winter Solstice
    SE <- as.Date("2012-3-15",  format = "%Y-%m-%d") # Spring Equinox
    SS <- as.Date("2012-6-15",  format = "%Y-%m-%d") # Summer Solstice
    FE <- as.Date("2012-9-15",  format = "%Y-%m-%d") # Fall Equinox

    # Convert dates from any year to 2012 dates
    d <- as.Date(strftime(DATES, format="2012-%m-%d"))

    ifelse (d >= WS | d < SE, "Winter",
      ifelse (d >= SE & d < SS, "Spring",
        ifelse (d >= SS & d < FE, "Summer", "Fall")))
}

my.dates <- as.Date("2011-12-01", format = "%Y-%m-%d") + 0:60
head(getSeason(my.dates), 24)
#  [1] "Fall"   "Fall"   "Fall"   "Fall"   "Fall"   "Fall"   "Fall"  
#  [8] "Fall"   "Fall"   "Fall"   "Fall"   "Fall"   "Fall"   "Fall"  
# [15] "Winter" "Winter" "Winter" "Winter" "Winter" "Winter"

One note: 2012 is a good year to which to convert all of the dates; since it is a leap year, any February 29ths in your data set will be handled smoothly.

这篇关于查找特定日期所属的季节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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