I have a data.table with a mix of dates, sfc, string, and numerical data that reflect the span of time the events lasted and I would like to add a column to reflect the weeks in which each event was present. While this worked previously when I broke down it down by having a row for every day for which the events occurred, it's not working now. I suspect it's due to missing values in the start/ end date columns.
Here's an example of my data:
#Required packages
require(data.table)
require(sf)
#Data
ID<-c(1:5)
lon<-c(-86.9655633, -85.8316318, -85.8316318, -82.8316318,-81.8316318)
lat<-c(34.5967589, 33.6598257, 33.6598257, 31.6598257, 32.6598257)
Country<-c("Malaysia", "Malayisia", "Singapore", "Thailand", "Thailand")
City<-c("Penang", "Malacca", "Singapore", "Bangkok", "Chiang Mai")
area_km2<-c(1048, 277, 728, 1569, 40)
start_date<-as.Date(c("2000-01-31", "2000-09-21", "2001-03-17", NA, NA))
end_date<-as.Date(c("2000-02-14", "2000-10-12", "2001-05-27", NA, NA))
samp<-data.table(ID, lon, lat, Country, City, area_km2, start_date, end_date)
samp <- st_as_sf(samp, coords = c("lon", "lat"), na.fail=F)
What I want is essentially to have one row for every week the event occurred in. E.g. if the event was from 3 Feb 2000 (Thur) to 8 Feb 2000 (Tue), I'd like to have two rows for the week starting 31 Jan, and another starting 7 Feb. Here is what I've tried so far which had worked previously.
samp = samp[, list(
`Week Starting` = if (!is.na(start_date) & !is.na(end_date) & start_date<=end_date)
{seq.Date(start_date, end_date, by = 'week')}
else {as.Date(NA)}),
by = list(ID, lon, lat, Country, City, area_km2)]
It's a little complicated, but essentially I am attempting to build a panel for all cities and weeks, even if no events occurred in those times/ locations. Let me know how I can clairfy further. Thanks!