If in date range then assign value [duplicate] - r

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 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",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice"))
seasonality <- data.frame(seastime, seas)
Ultimately, I would like my new data table to look something like this:
Date.time Season
2012-12-20 19:28:00 Winter
2012-12-25 21:08:00 Winter
2012-12-29 02:50:00 Winter
2013-01-01 12:32:00 Winter
2013-01-11 05:24:00 Winter
2013-02-11 06:32:00 Winter
2013-02-12 22:06:00 Winter
2013-02-16 11:42:00 Winter
2013-02-19 11:06:00 Winter
2013-02-22 13:10:00 Winter
2013-02-26 05:30:00 Winter
2013-03-12 02:24:00 Winter
2013-03-18 21:54:00 Winter
2013-03-24 04:50:00 Winter
2013-03-31 05:54:00 Spring
2013-04-01 03:52:00 Spring
2013-04-04 12:34:00 Spring

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))

Related

as.Date() not working with %B on Mac maybe? [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 2 years ago.
I don't know what I'm doing wrong with my date data. A sample of the Date column in the housing_data data frame are as follows:
"February 2012" "March 2012" "April 2012" "May 2013" "July 2015" "March 2016"
"May 2016" "April 2017" "July 2017" "October 2017" "December 2017" "February 2018"
I run housing_data$Date <- base::as.Date(housing_data$Date, "%B %Y") and I get nothing but NA back as if I have the formatting incorrect. What am I missing?
I would suggest adding a day value to your string in this way:
#Data
x1 <- c("February 2012", "March 2012", "April 2012", "May 2013", "July 2015")
Code:
#For date
as.Date(paste('01',x1),'%d %B %Y')
Output:
[1] "2012-02-01" "2012-03-01" "2012-04-01" "2013-05-01" "2015-07-01"
Or you can try:
#Format date
format(as.Date(paste('01',x1),'%d %B %Y'),'%B %Y')
Output:
[1] "February 2012" "March 2012" "April 2012" "May 2013" "July 2015"

For loop help (R)

I would like to have a for loop for this:
months = c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
years = c(2018, 2019)
input = 17
for (i in 1:input) {
output[i] = paste(months[i], years[i], sep = " ")????
NEED HELP HERE. rep() ???
}
And I would like the output to be a vector that consists on 17 months:
Output = c("January 2018", "February 2018", "March 2018", "April 2018", ... , "May 2019")
Thank you very much for your help.
Another option will be this:
> c(outer(month.name, 2018:2019, paste))[1:17]
[1] "January 2018" "February 2018" "March 2018"
[4] "April 2018" "May 2018" "June 2018"
[7] "July 2018" "August 2018" "September 2018"
[10] "October 2018" "November 2018" "December 2018"
[13] "January 2019" "February 2019" "March 2019"
[16] "April 2019" "May 2019"
There's already a system supplied vector of month names: month.name. Since paste is vectorized and does recycling, there's no need for a for loop and the default separator for paste is " ", so the code could just be:
output <- paste( month.name, rep( years, each=12) )[1:17]
# test result ----
> output
[1] "January 2018" "February 2018" "March 2018" "April 2018" "May 2018" "June 2018"
[7] "July 2018" "August 2018" "September 2018" "October 2018" "November 2018" "December 2018"
[13] "January 2019" "February 2019" "March 2019" "April 2019"
The other way to do it would be with format applied to seq.Date results:
output <- format( seq( as.Date('2018-01-01'), as.Date('2019-04-01'), by="month") ,
"%B %Y" ) # argument to the format parameter for output
#---------------------
> output
[1] "January 2018" "February 2018" "March 2018" "April 2018" "May 2018" "June 2018"
[7] "July 2018" "August 2018" "September 2018" "October 2018" "November 2018" "December 2018"
[13] "January 2019" "February 2019" "March 2019" "April 2019"
See ?seq.Date and ?format.Date
c(paste(months,"2018"),paste(months,"2019"))[1:17]
## [1] "January 2018" "February 2018" "March 2018" "April 2018" "May 2018" "June 2018"
## [7] "July 2018" "August 2018" "September 2018" "October 2018" "November 2018" "December 2018"
## [13] "January 2019" "February 2019" "March 2019" "April 2019" "May 2019"

Sequence of Months in Words r?

Let's say I want to have a vector that contains each month in order starting with March 2012. I want that month and the next 33.
How would I do this?
With base R:
dates <- seq.Date(from = as.Date("2012-03-01"), length.out = 33, by = "month")
format(dates, "%B %Y")
#> [1] "March 2012" "April 2012" "May 2012" "June 2012"
#> [5] "July 2012" "August 2012" "September 2012" "October 2012"
#> [9] "November 2012" "December 2012" "January 2013" "February 2013"
#> [13] "March 2013" "April 2013" "May 2013" "June 2013"
#> [17] "July 2013" "August 2013" "September 2013" "October 2013"
#> [21] "November 2013" "December 2013" "January 2014" "February 2014"
#> [25] "March 2014" "April 2014" "May 2014" "June 2014"
#> [29] "July 2014" "August 2014" "September 2014" "October 2014"
#> [33] "November 2014"
Created on 2019-01-27 by the reprex package (v0.2.1)
I found this
You can do this:
df <- ymd("2012-03-01")+ months(0:33)
df
Give me:
[1] "2012-03-01" "2012-04-01" "2012-05-01" "2012-06-01" "2012-07-01" "2012-08-01" "2012-09-01"
[8] "2012-10-01" "2012-11-01" "2012-12-01" "2013-01-01" "2013-02-01" "2013-03-01" "2013-04-01"
[15] "2013-05-01" "2013-06-01" "2013-07-01" "2013-08-01" "2013-09-01" "2013-10-01" "2013-11-01"
[22] "2013-12-01" "2014-01-01" "2014-02-01" "2014-03-01" "2014-04-01" "2014-05-01" "2014-06-01"
[29] "2014-07-01" "2014-08-01" "2014-09-01" "2014-10-01" "2014-11-01" "2014-12-01

Using '\\s' vs ' ' in regular expressions, is '\\s' looking for space and backslash simultaneously?

Say I use the string below:
x <- "County January 2016 February 2016 March 2016 April 2016 May 2016 June 2016 July 2016 August 2016 September 2016 October 2016 November 2016 December 2016\r"
What I gather from running the two commands below is that '\s' will seek out a ' ' (space) as well as a '\' backslash.
strsplit(x, "(?<=2016) +", perl=T)
[[1]]
[1] "County January 2016"
[2] "February 2016"
[3] "March 2016"
[4] "April 2016"
[5] "May 2016"
[6] "June 2016"
[7] "July 2016"
[8] "August 2016"
[9] "September 2016"
[10] "October 2016"
[11] "November 2016"
[12] "December 2016\r"
strsplit(x, "(?<=2016)\\s+", perl=T)
[[1]]
[1] "County January 2016"
[2] "February 2016"
[3] "March 2016"
[4] "April 2016"
[5] "May 2016"
[6] "June 2016"
[7] "July 2016"
[8] "August 2016"
[9] "September 2016"
[10] "October 2016"
[11] "November 2016"
[12] "December 2016"
Since the second command also splits on '\' following the last 2016. Am I understanding this correct? I appreciate the help thanks.

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 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",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice",
"December Solstice"))
seasonality <- data.frame(seastime, seas)
Ultimately, I would like my new data table to look something like this:
Date.time Season
2012-12-20 19:28:00 Winter
2012-12-25 21:08:00 Winter
2012-12-29 02:50:00 Winter
2013-01-01 12:32:00 Winter
2013-01-11 05:24:00 Winter
2013-02-11 06:32:00 Winter
2013-02-12 22:06:00 Winter
2013-02-16 11:42:00 Winter
2013-02-19 11:06:00 Winter
2013-02-22 13:10:00 Winter
2013-02-26 05:30:00 Winter
2013-03-12 02:24:00 Winter
2013-03-18 21:54:00 Winter
2013-03-24 04:50:00 Winter
2013-03-31 05:54:00 Spring
2013-04-01 03:52:00 Spring
2013-04-04 12:34:00 Spring
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))

Resources