rccdates.Rmd
Registers managed by the Swedish Cancer centers (quality registers and the cancer register) can have date variables in different formats. This is most evident for old registers handled by Rockan.
as.Dates
functionThe function as.Dates
(plural) is intended to replace the ordinary as.Date
(singular) function from base R.
RCC dates are usually in the form %Y-%m-%d
, such as 2020-04-09. These are recognized by ordinary R-functions such as as.Date
if there are no missing values or if missing values are coded as NA
. It is however common for RCC that missing dates are coded as empty strings:
library(incadata)
## Loading required package: decoder
## Error in charToDate(x): character string is not in a standard unambiguous format
as.Dates
on the other hand has no problem:
as.Dates(d)
## [1] NA "2016-06-17"
Date from old Rockan registers can also have missing days and months (coded as “00”). as.Dates
will recognize these dates and will replace “00” by an approximate date:
as.Date("2000-01-00") # as.Date fails!
## Error in charToDate(x): character string is not in a standard unambiguous format
as.Dates("2000-01-00") # Missing day
## [1] "2000-01-15"
as.Dates("2000-00-00") # Missing month and day
## [1] "2000-07-01"
Some old dates can have format %Y%V
(see ?strptime
), such as “7403” for week 3 in 1974. This is tricky for four reasons:
as.Date("7403")
## Error in charToDate(x): character string is not in a standard unambiguous format
as.Dates("7403")
## [1] "1974-01-17"
It is also possible to have a mixture of different dates within the same vector:
## [1] NA NA "2000-01-01" "2000-01-01" "2000-07-01"
## [6] "1974-01-17"