The issue is likely due to as.POSIXct() interpreting your call_month column incorrectly. If call_month is already a date (e.g., "2020-01-01"), converting it to POSIXct (which includes time) might introduce unexpected behavior, such as timezone adjustments or incorrect parsing.
Solution: Keep call_month as a Date (not POSIXct)
If you only need dates (not timestamps), use as.Date() instead of as.POSIXct():
sdid_seattle <- sdid_seattle_no_dumping_detail %>%
left_join(dumping_materials_month, by = "call_month") %>%
select(
call_month, call_count, encampment_call_count, drug_needles_call_count,
homeless_call_count, dumping_call_count, unique_encampment_100m,
unique_encampment_150m, unique_encampment_200m
) %>%
mutate(call_month = as.Date(call_month)) # Use as.Date() instead of as.POSIXct()
If You Really Need POSIXct (with Time)
If you must use POSIXct (e.g., for time-based operations), ensure the format is correct:
sdid_seattle <- sdid_seattle %>%
mutate(call_month = as.POSIXct(call_month, format = "%Y-%m-%d")) # Explicit format
Why This Happens
as.POSIXct() assumes a default format (often "%Y-%m-%d %H:%M:%S"), which can misparse dates.
- If
call_month is already a Date object, as.POSIXct() might convert it to a timestamp at midnight UTC, which could shift dates if your system timezone is different.
Best Practice
- Use
as.Date() for dates only.
- Use
as.POSIXct() only if you need time components (hours, minutes, seconds).