Skip to content

Commit

Permalink
Ignore NA values in date conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
sjewo authored and JanMarvin committed Sep 23, 2023
1 parent ba164ba commit 7787c84
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions R/convert.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ convert_dt_m <- function(x) {
mth <- x %% 12 + 1
yr <- 1960 + floor(z)

z <- paste0(yr, "-", mth, "-1")
z <- ifelse(is.na(z), NA, paste0(yr, "-", mth, "-1"))
z <- as.Date(z, "%Y-%m-%d")
if (any(is.na(z))) warning("conversion of %tm failed")
z
}

Expand All @@ -58,9 +57,8 @@ convert_dt_q <- function(x) {
qrt <- x %% 4 + 1
qrt_month <- c(1, 4, 7, 10)

z <- paste0(yr, "-", qrt_month[qrt], "-1")
z <- ifelse(is.na(z), NA, paste0(yr, "-", qrt_month[qrt], "-1"))
z <- as.Date(z, "%Y-%m-%d")
if (any(is.na(z))) warning("conversion of %tq failed")
z
}

Expand All @@ -71,7 +69,7 @@ convert_dt_q <- function(x) {
# @author Jan Marvin Garbuszus \email{jan.garbuszus@@ruhr-uni-bochum.de}
# @author Sebastian Jeworutzki \email{sebastian.jeworutzki@@ruhr-uni-bochum.de}
convert_dt_y <- function(x) {
z <- as.Date(paste0(x, "-1-1"), "%Y-%m-%d")
if (any(is.na(z))) warning("conversion of %ty failed")
z <- ifelse(is.na(x), NA, paste0(x, "-1-1"))
z <- as.Date(z, "%Y-%m-%d")
z
}

0 comments on commit 7787c84

Please sign in to comment.