问题描述
好的,我经历了几个问题,我认为我的数据中有一个可以解决的模式(也就是说,我可以区分dmy和mdy)。
让我为数据集提供一些背景知识。数据集包含有关当前隔离期间出行人员的信息。因此,当我向下滚动数据集时,我看到大多数日期是以M-D-Y格式输入的,而有些则是以dmy格式输入的。
因此对于此数据集,(数据集包含截至八月的信息)。歧义可以通过以下方式解决:
- 日期不能在将来。因此,这解决了08-12-2020和12-08-2020之间的差异。而且,我只有直到六月的数据。
- 日子比几个月快。如果我看到一个序列(无论是dmy还是mdy),如果我看到每隔几行就有一个数字变化(比如说20),那么我知道变化的数字是一天而不是一个月。 示例:
在这种情况下如何正确分配日期?
解决方法
因此,如果我理解正确,则您的数据按时间顺序排列,并且您对测量数据的时间范围有所了解。
有鉴于此,这是我创建加扰数据然后重新解析它的目的:
library(dplyr)
library(lubridate)
library(zoo) # for na.locf function
# create some scrambled data to work with
df <- tibble(
date_ground_truth = rep(seq(from = ymd('20190801'),to = ymd('20200730'),by = 1),each = 5)
) %>%
mutate(
date_inconsistent_chr = format(date_ground_truth,ifelse(runif(nrow(.),1) > 0.3,'%Y/%m/%d','%Y/%d/%m'))
)
# providing here the date range in which your observations lie. I only know that it maxes end of July 2020,so my end result has some remaining unknowns at the start
daterange_known_min <- NA_Date_
daterange_known_max <- ymd('20200730')
# initiate a cleaned df - for any date where we have a day > 12,we know that it can only be one format of YMD/YDM
df_recleaned <- df %>%
mutate(
date_parsed_ymd = as.Date(date_inconsistent_chr,'%Y/%m/%d'),# try YMD
date_parsed_ydm = as.Date(date_inconsistent_chr,'%Y/%d/%m'),# try YDM
date_parsed_deducted = case_when( # write out the clear cut cases
day(date_parsed_ymd) > 12 ~ date_parsed_ymd,day(date_parsed_ydm) > 12 ~ date_parsed_ydm,date_parsed_ymd == date_parsed_ydm ~ date_parsed_ymd,T ~ NA_Date_
)
)
# we will run over the data until we can not deduct any more new dates from what we've learnt so far:
new_guesses_possible <- T
while(new_guesses_possible) {
# how many dates did we already deduct?
num_deducted_dates_before <- df_recleaned %>% filter(!is.na(date_parsed_deducted)) %>% nrow()
# deduct more,based on our knowledge that the dates are chronological and within a certain time-frame
df_recleaned <- df_recleaned %>%
mutate(
earliest_possible_date = coalesce(na.locf(date_parsed_deducted,na.rm = F),daterange_known_min),last_possible_date = coalesce(na.locf(date_parsed_deducted,fromLast = T,daterange_known_max),ymd_guess_in_range = coalesce(date_parsed_ymd >= earliest_possible_date & date_parsed_ymd <= last_possible_date,F),ydm_guess_in_range = coalesce(date_parsed_ydm >= earliest_possible_date & date_parsed_ydm <= last_possible_date,date_parsed_deducted = case_when(
# keep clear cases
!is.na(date_parsed_deducted) ~ date_parsed_deducted,# if the ymd-guess falls between the last clear case and next clear case,take ymd
ymd_guess_in_range & !ydm_guess_in_range ~ date_parsed_ymd,# same approach for ydm
ydm_guess_in_range & !ymd_guess_in_range ~ date_parsed_ydm,# cover the cases where we don't know either the last or next clear parsed date
# if one of the parsed dates falls before the "first possible date",take the other one.
# (if no daterange_known_min is given,these rows will result in NA and not do anything...)
date_parsed_ymd < daterange_known_min ~ date_parsed_ydm,date_parsed_ydm < daterange_known_min ~ date_parsed_ymd,# inversely,if one parsed option falls after the "last possible date",ignore it.
date_parsed_ymd > daterange_known_max ~ date_parsed_ydm,date_parsed_ydm > daterange_known_max ~ date_parsed_ymd
)
)
# how many dates did we now deduct?
num_deducted_dates_after <- df_recleaned %>% filter(!is.na(date_parsed_deducted)) %>% nrow()
# do we need to go on?
new_guesses_possible <- num_deducted_dates_after > 0 & num_deducted_dates_before != num_deducted_dates_after
}
# kick out all those extra columns :)
df_recleaned_final <- df_recleaned %>%
select(
-date_parsed_ymd,-date_parsed_ydm,-earliest_possible_date,-last_possible_date,-ymd_guess_in_range,-ydm_guess_in_range
)
在我的示例中,这修复了2019年8月第一周之后的所有日期。
如果数据之间的间隔更长,它可能会给您带来不同的结果。