Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

r - How to classify a given date/time by the season (e.g. summer, autumn)

I have a list of data with date/time information. Here is a sample:

Date.time                
2012-12-20 19:28:00
2012-12-25 21:08:00
2012-12-29 02:50:00
2013-01-01 12:32:00
2013-01-11 05:24:00
2013-02-11 06:32:00
2013-02-12 22:06:00
2013-02-16 11:42:00
2013-02-19 11:06:00
2013-02-22 13:10:00
2013-02-26 05:30:00
2013-03-12 02:24:00
2013-03-18 21:54:00
2013-03-24 04:50:00
2013-03-31 05:54:006    
2013-04-01 03:52:00
2013-04-04 12:34:00

I also have a separate table which defines by date and time the different equinoxes and solstices for the next 20 years. Similar to a Vlookup, can I cross-reference that seasonal table based on the above input file, and end up adding a factor column to this raw data that has the season within which the time and date lies?

Cross-reference season definition table:

seastime <- c("03/20/2000 02:35",
          "03/20/2001 08:31",
          "03/20/2002 14:16",
          "03/20/2003 20:00",
          "03/20/2004 01:48",
          "03/20/2005 07:33",
          "03/20/2006 13:25",
          "03/20/2007 20:07",
          "03/20/2008 01:48",
          "03/20/2009 07:44",
          "03/20/2010 13:32",
          "03/20/2011 19:21",
          "03/20/2012 01:14",
          "03/20/2013 07:02",
          "03/20/2014 00:57",
          "03/20/2015 18:45",
          "03/20/2016 00:30",
          "03/20/2017 06:29",
          "03/20/2018 00:15",
          "03/20/2019 17:58",
          "03/19/2020 23:50",
          "03/20/2021 05:37",
          "03/20/2022 23:33",
          "03/20/2023 17:24",
          "03/19/2024 23:06",
          "03/20/2025 05:01",
          "03/20/2026 22:45",
          "03/20/2027 16:25",
          "03/19/2028 22:17",
          "03/20/2029 04:01",
          "03/20/2030 09:52",
          "03/20/2031 15:41",
          "03/19/2032 21:22",
          "03/20/2033 03:23",
          "03/20/2034 09:17",
          "03/20/2035 15:03",
          "03/19/2036 21:02",
          "03/20/2037 02:50",
          "03/20/2038 08:40",
          "03/20/2039 14:32",
          "03/19/2040 20:11",
          "03/20/2041 02:06",
          "03/20/2042 07:53",
          "03/20/2043 13:27",
          "03/19/2044 19:20",
          "03/20/2045 01:07",
          "03/20/2046 06:58",
          "03/20/2047 00:52",
          "03/19/2048 18:33",
          "03/20/2049 00:28",
          "06/20/2000 21:48",
          "06/21/2001 03:38",
          "06/21/2002 09:25",
          "06/21/2003 15:11",
          "06/20/2004 20:57",
          "06/21/2005 02:46",
          "06/21/2006 08:26",
          "06/21/2007 14:06",
          "06/20/2008 19:59",
          "06/21/2009 01:46",
          "06/21/2010 07:29",
          "06/21/2011 13:16",
          "06/20/2012 19:08",
          "06/21/2013 01:04",
          "06/21/2014 06:51",
          "06/21/2015 00:38",
          "06/20/2016 18:34",
          "06/21/2017 00:24",
          "06/21/2018 06:07",
          "06/21/2019 23:54",
          "06/20/2020 17:43",
          "06/20/2021 23:32",
          "06/21/2022 05:14",
          "06/21/2023 22:57",
          "06/20/2024 16:51",
          "06/20/2025 22:42",
          "06/21/2026 04:25",
          "06/21/2027 22:11",
          "06/20/2028 16:02",
          "06/20/2029 21:48",
          "06/21/2030 03:31",
          "06/21/2031 09:17",
          "06/20/2032 15:09",
          "06/20/2033 21:01",
          "06/21/2034 02:44",
          "06/21/2035 08:33",
          "06/20/2036 14:32",
          "06/20/2037 20:22",
          "06/21/2038 02:09",
          "06/21/2039 07:57",
          "06/20/2040 13:46",
          "06/20/2041 19:36",
          "06/21/2042 01:16",
          "06/21/2043 06:58",
          "06/20/2044 00:51",
          "06/20/2045 18:33",
          "06/21/2046 00:15",
          "06/21/2047 06:03",
          "06/20/2048 23:54",
          "06/20/2049 17:47",
          "09/22/2000 13:28",
          "09/22/2001 19:05",
          "09/23/2002 00:55",
          "09/23/2003 06:47",
          "09/22/2004 00:30",
          "09/22/2005 18:23",
          "09/23/2006 00:04",
          "09/23/2007 05:51",
          "09/22/2008 23:44",
          "09/22/2009 17:19",
          "09/22/2010 23:09",
          "09/23/2011 05:04",
          "09/22/2012 22:49",
          "09/22/2013 16:44",
          "09/22/2014 22:29",
          "09/23/2015 04:20",
          "09/22/2016 22:21",
          "09/22/2017 16:02",
          "09/22/2018 21:54",
          "09/23/2019 03:50",
          "09/22/2020 09:31",
          "09/22/2021 15:21",
          "09/22/2022 21:04",
          "09/23/2023 02:50",
          "09/22/2024 08:44",
          "09/22/2025 14:20",
          "09/22/2026 20:05",
          "09/23/2027 02:01",
          "09/22/2028 07:45",
          "09/22/2029 13:38",
          "09/22/2030 19:27",
          "09/23/2031 01:15",
          "09/22/2032 07:11",
          "09/22/2033 00:52",
          "09/22/2034 18:39",
          "09/23/2035 00:39",
          "09/22/2036 06:24",
          "09/22/2037 00:13",
          "09/22/2038 18:02",
          "09/22/2039 23:49",
          "09/22/2040 05:45",
          "09/22/2041 23:26",
          "09/22/2042 17:11",
          "09/22/2043 23:06",
          "09/22/2044 04:48",
          "09/22/2045 22:33",
          "09/22/2046 16:22",
          "09/22/2047 22:08",
          "09/22/2048 04:00",
          "09/22/2049 09:42",
          "12/21/2000 08:38",
          "12/21/2001 14:21",
          "12/21/2002 20:14",
          "12/22/2003 02:04",
          "12/21/2004 07:42",
          "12/21/2005 13:35",
          "12/21/2006 19:22",
          "12/22/2007 01:07",
          "12/21/2008 07:04",
          "12/21/2009 00:47",
          "12/21/2010 18:38",
          "12/22/2011 00:30",
          "12/21/2012 06:12",
          "12/21/2013 00:11",
          "12/21/2014 18:03",
          "12/21/2015 23:48",
          "12/21/2016 05:44",
          "12/21/2017 23:28",
          "12/21/2018 17:22",
          "12/21/2019 23:19",
          "12/21/2020 05:02",
          "12/21/2021 22:59",
          "12/21/2022 16:48",
          "12/21/2023 22:27",
          "12/21/2024 04:20",
          "12/21/2025 22:03",
          "12/21/2026 15:50",
          "12/21/2027 21:42",
          "12/21/2028 03:20",
          "12/21/2029 09:14",
          "12/21/2030 15:09",
          "12/21/2031 20:55",
          "12/21/2032 02:56",
          "12/21/2033 08:45",
          "12/21/2034 14:34",
          "12/21/2035 20:31",
          "12/21/2036 02:13",
          "12/21/2037 08:07",
          "12/21/2038 14:02",
          "12/21/2039 19:40",
          "12/21/2040 01:33",
          "12/21/2041 07:18",
          "12/21/2042 13:04",
          "12/21/2043 19:01",
          "12/21/2044 00:44",
          "12/21/2045 06:35",
          "12/21/2046 00:28",
          "12/21/2047 18:07",
          "12/21/2048 00:02",
          "12/21/2049 05:52")

seastime <- as.POSIXct(as.character(seastime), format="%m/%d/%Y %H:%M")

seas <- as.factor(c("March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "March Equinox",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "June Solstice",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September Equinox",
                "September 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here are three possibilities, one using cut, as suggested by @Spacedman in the comments, a second using findInterval, and a third which uses a rolling join in data.table. As discussed in the comments above, solstice and equinox dates are defined as onset of a Season. This may explain the difference between my output and OP:s (not yet edited) desired result. I leave the mapping of 'seas' to more sensible season names to you.

1. cut

Date.time$seastime <- as.POSIXct(cut(Date.time$datetime, seasonality$seastime))
Date.time <- merge(Date.time, seasonality)
Date.time[ , c("datetime", "seas")] 
#               datetime              seas
# 1  2012-12-20 19:28:00 September Equinox
# 2  2012-12-25 21:08:00 December Solstice
# 3  2012-12-29 02:50:00 December Solstice
# snip
# 12 2013-03-12 02:24:00 December Solstice
# 13 2013-03-18 21:54:00 December Solstice
# 14 2013-03-24 04:50:00     March Equinox
# 15 2013-03-31 05:54:06     March Equinox
# 16 2013-04-01 03:52:00     March Equinox
# 17 2013-04-04 12:34:00     March Equinox

2. findInterval

seasonality <- seasonality[order(seasonality$seastime), ]
Date.time$seas <- seasonality$seas[findInterval(x = Date.time$datetime, vec = seasonality$seastime)]
Date.time
#               datetime              seas
# 1  2012-12-20 19:28:00 September Equinox
# 2  2012-12-25 21:08:00 December Solstice
# 3  2012-12-29 02:50:00 December Solstice
# snip
# 12 2013-03-12 02:24:00 December Solstice
# 13 2013-03-18 21:54:00 December Solstice
# 14 2013-03-24 04:50:00     March Equinox
# 15 2013-03-31 05:54:06     March Equinox
# 16 2013-04-01 03:52:00     March Equinox
# 17 2013-04-04 12:34:00     March Equinox   

3. data.table rolling join

library(data.table)
setDT(Date.time)
setDT(seasonality)

setkey(Date.time, datetime)
setkey(seasonality, seastime)

seasonality[Date.time, roll = Inf]
#                seastime              seas
# 1:  2012-12-20 19:28:00 September Equinox
# 2:  2012-12-25 21:08:00 December Solstice
# 3:  2012-12-29 02:50:00 December Solstice
# snip
# 12: 2013-03-12 02:24:00 December Solstice
# 13: 2013-03-18 21:54:00 December Solstice
# 14: 2013-03-24 04:50:00     March Equinox
# 15: 2013-03-31 05:54:06     March Equinox
# 16: 2013-04-01 03:52:00     March Equinox
# 17: 2013-04-04 12:34:00     March Equinox

# possibly rename 'seastime' here.

'Date.time' used here:

structure(list(datetime = structure(c(1356028080, 1356466080, 
1356745800, 1357039920, 1357878240, 1360560720, 1360703160, 1361011320, 
1361268360, 1361535000, 1361853000, 1363051440, 1363640040, 1364097000, 
1364702046, 1364781120, 1365071640), class = c("POSIXct", "POSIXt"
), tzone = "")), .Names = "datetime", class = "data.frame", row.names = c(NA, 
-17L))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...