Q:

How can I create a function to get a date-time subset of a second dataframe?

I'm trying to write code to partially automate the process of subsetting a temperature dataset with start/end date-times in a winter mortality dataset, the latter having something over 100 observations, each of which would end up with one such temp data subset.  I plan to calculate some temperature variables using each of these subsets, and add them to this second dataset.. but I'm hung up on the subsetting step.  
Here's example data and my code (and let me know if you have suggestions on making this minimum reproducible.. I haven't posted here too much yet):
# Temperature data dput..
tempd <- structure(list(date = structure(c(1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L), .Label = c("12/1/2014", "12/2/2014", "12/3/2014", "12/4/2014", "12/5/2014", "12/6/2014"), class = "factor"), time = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("0:00:00", "12:00:00"), class = "factor"), temp = c(3.274, -0.986, -0.088, 0.495, 6.23, 0.934, 0.715, -4.227, -1.584, 0.88, 1.967)), .Names = c("date", "time", "temp"), class = "data.frame", row.names = c(NA, -11L))

# and mortality data dput..
owmd <- structure(list(siteyear = structure(c(1L, 1L, 1L), .Label = "s1.y1", class = "factor"), winter = c(1415L, 1415L, 1415L), date = structure(1:3, .Label = c("12/1/2014", "12/3/2014", "12/5/2014"), class = "factor"), site = structure(c(1L, 1L, 1L), .Label = "s1", class = "factor"), mort = c(0.06651485, 0.120592869, 0.135272089)), .Names = c("siteyear", "winter", "date", "site", "mort"), class = "data.frame", row.names = c(NA, -3L))

EDIT:
In case I've oversimplified my temp dataset, I'll say that my real temperature datasets (there are 10 of them, one for each site-year combination I have) contain values of temperature at 15 minute intervals (i.e. 96/day).  Importantly, I want these temp subsets to start and end at 12pm, so I need to be able to specify the time as well as the date at the subset limits (note the very first temp subset of the dataset might not be able to start at 12pm if the dataset itself begins at a later time)
So, the code..
library(tidyverse)
library(lubridate)

# Factorize winter and 'date-ize' date 
owmd$winter <- as.factor(owmd$winter)
owmd$date <- as.Date(owmd$date, '%m/%d/%Y')

# Create start date (date value for the prior observation)
owmd %>%
  tbl_df() %>%
  mutate(sdate = lag(date, 1)) -> owmd

# Now the temperature dataset

# Factorize date, do *something* with time, and create datetime
tempd$date2 <- as.Date(tempd$date, '%m/%d/%Y')
tempd %>%
  mutate(datetime = ymd_hms('2014-12-01 12:00:00') + c(0:10) * hours(12), 
         time2 = parse_time(tempd$time)) -> tempd

# write a function that creates, for each observation in owmd, a subset of the tempd data bounded by owmd$date and owmd$sdate ('start date')
subfun <- function(x,y) {
  start <- owmd[(x-1),3]
  end <- owmd[x,3]
  period <- filter(y, date2 >= start & time2 >= '12:00:00' & date2 <= end & time2 <= '12:00:00')
}

# test it
subfun(3, tempd)

Finding the right subset conditions in period is where I'm hung up.  I'm getting 
Warning messages:
1: In evalq((date2 >= start & time2 >= "12:00:00") & (date2 <= end &  :
  Incompatible methods (">=.Date", "Ops.data.frame") for ">="
2: In evalq((date2 >= start & time2 >= "12:00:00") & (date2 <= end &  :
  Incompatible methods ("<=.Date", "Ops.data.frame") for "<="

Seems like it shouldn't be too hard to use owmd$date and owmd$sdate (start date) as bounds for temperature dataset subsets, but I haven't managed to figure out the right subset conditions.  Would a different format for tempd$time help?  I include temp$datetime in case it could be used, but I didn't see how.
Any thoughts for a beginner are greatly appreciated.
Here's my session info: 

R version 3.4.4 (2018-03-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

A:

Thanks for providing reproducible data! There are a number of fixes that can help make this easier and more readable; below is how I would approach it. Essentially your goal is to figure out which temperature measurements fit each mortality period to do statistics on them. There's a few key realisations:

You want a function that accepts a dataframe, a column to put conditions on, and two conditions, but this is already what dplyr::filter does so we don't need another wrapper function really;
It would be helpful to keep the subsets with the corresponding mortality data and start/end dates so we should use a list-column to hold it;
We can concatenate 12:00:00 to the dates in owmd before parsing so that they are datetimes;
We can do a bunch of style changes (e.g. stick to lubridate parsers since you already use the package)

This means that I can do this:
library(tidyverse)
library(lubridate)
tempd <- structure(list(date = structure(c(1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L), .Label = c("12/1/2014", "12/2/2014", "12/3/2014", "12/4/2014", "12/5/2014", "12/6/2014"), class = "factor"), time = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("0:00:00", "12:00:00"), class = "factor"), temp = c(3.274, -0.986, -0.088, 0.495, 6.23, 0.934, 0.715, -4.227, -1.584, 0.88, 1.967)), .Names = c("date", "time", "temp"), class = "data.frame", row.names = c(NA, -11L))
owmd <- structure(list(siteyear = structure(c(1L, 1L, 1L), .Label = "s1.y1", class = "factor"), winter = c(1415L, 1415L, 1415L), date = structure(1:3, .Label = c("12/1/2014", "12/3/2014", "12/5/2014"), class = "factor"), site = structure(c(1L, 1L, 1L), .Label = "s1", class = "factor"), mort = c(0.06651485, 0.120592869, 0.135272089)), .Names = c("siteyear", "winter", "date", "site", "mort"), class = "data.frame", row.names = c(NA, -3L))

mort <- owmd %>%
  mutate(
    winter = factor(winter),
    date = date %>% str_c(., " 12:00:00") %>% mdy_hms(),
    sdate = lag(date)
  )
temp <- tempd %>%
  mutate(datetime = str_c(date, " ", time) %>% mdy_hms(), date = mdy(date))

mort_subs <- mort %>%
  as_tibble() %>%
  mutate(
    subset = map2(sdate, date, ~ filter(temp, datetime >= .x & datetime <= .y)
    )
  )
mort_subs
#> # A tibble: 3 x 7
#>   siteyear winter date                site    mort sdate              
#>   <fct>    <fct>  <dttm>              <fct>  <dbl> <dttm>             
#> 1 s1.y1    1415   2014-12-01 12:00:00 s1    0.0665 NA                 
#> 2 s1.y1    1415   2014-12-03 12:00:00 s1    0.121  2014-12-01 12:00:00
#> 3 s1.y1    1415   2014-12-05 12:00:00 s1    0.135  2014-12-03 12:00:00
#> # ... with 1 more variable: subset <list>

so now we have the right subset for each original row of owmd. Each element of the subset column contains a dataframe that is the subset of temp corresponding to the sdate and date. The tidyverse has a lot of tools that let us work with this data easily; we can unnest, group_by and summarise to get a statistic per original row:
mort_subs %>%
  unnest() %>%
  group_by(siteyear, winter, sdate, date, site, mort) %>%
  summarise(mean_temp = mean(temp))
#> # A tibble: 2 x 7
#> # Groups:   siteyear, winter, sdate, date, site [?]
#>   siteyear winter sdate               date                site   mort
#>   <fct>    <fct>  <dttm>              <dttm>              <fct> <dbl>
#> 1 s1.y1    1415   2014-12-01 12:00:00 2014-12-03 12:00:00 s1    0.121
#> 2 s1.y1    1415   2014-12-03 12:00:00 2014-12-05 12:00:00 s1    0.135
#> # ... with 1 more variable: mean_temp <dbl>

Or we can accomplish a similar thing by using map_dbl to iterate over the subset list-column:
mort_subs %>%
  mutate(mean_temp = map_dbl(subset, ~ mean(.$temp)))
#> # A tibble: 3 x 8
#>   siteyear winter date                site    mort sdate              
#>   <fct>    <fct>  <dttm>              <fct>  <dbl> <dttm>             
#> 1 s1.y1    1415   2014-12-01 12:00:00 s1    0.0665 NA                 
#> 2 s1.y1    1415   2014-12-03 12:00:00 s1    0.121  2014-12-01 12:00:00
#> 3 s1.y1    1415   2014-12-05 12:00:00 s1    0.135  2014-12-03 12:00:00
#> # ... with 2 more variables: subset <list>, mean_temp <dbl>

Created on 2018-10-24 by the reprex package (v0.2.0).

