Skip to content

Commit

Permalink
fix add_duration error with tibble data input
Browse files Browse the repository at this point in the history
  • Loading branch information
andybega committed Jun 19, 2023
1 parent 334a0c7 commit 0ffeaea
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: spduration
Title: Split-Population Duration (Cure) Regression
Version: 0.17.1.9000
Version: 0.17.1.9002
Authors@R: c(
person("Andreas", "Beger", , "adbeger@gmail.com", c("aut", "cre"), comment = c(ORCID = "0000-0003-1883-3169")),
person("Daina", "Chiba", , "dchiba@essex.ac.uk", "aut"),
Expand Down Expand Up @@ -43,7 +43,7 @@ Suggests:
LinkingTo:
Rcpp,
RcppArmadillo
RoxygenNote: 7.1.0
RoxygenNote: 7.2.3
VignetteBuilder: knitr
URL: https://github.com/andybega/spduration, https://andybeger.com/spduration
BugReports: https://github.com/andybega/spduration/issues
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# spduration 0.17.2

- Fix `model.matrix` so that it works correctly with dots arguments.
- Fix an error when trying to use `add_duration()` with a tibble as input. The
errors were due to tibble returing lists when subsetting columns with `[, col]`.
The data input is now internally converted to a base data frame (`as.data.frame()`).
Expand Down
11 changes: 7 additions & 4 deletions R/add_duration.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,17 @@ add_duration <- function(data, y, unitID, tID, freq="month", sort=FALSE,

## Check input

# make sure it's not a tibble
data <- as.data.frame(data)

# valid frequency
supported.freq <- c("day", "month", "year")
if (!freq %in% supported.freq) {
stop("frequency must be 'day', 'month' or 'year'")
}

# convert to date if possible
if (class(data[, tID])!="Date") {
if (!inherits(data[[tID]], "Date")) {
data[, "temp.tID"] <- attempt_date(data[, tID], freq)
tID <- "temp.tID"
}
Expand All @@ -100,9 +103,9 @@ add_duration <- function(data, y, unitID, tID, freq="month", sort=FALSE,
}

# check that y is binary and has failures
if (!all(unique(data[, y]) %in% c(0, 1))) {
if (!all(unique(data[[y]]) %in% c(0, 1))) {
stop(paste(y, "must be binary (0, 1)"))
} else if (all(data[, y]==0)) {
} else if (all(data[[y]]==0)) {
stop(paste0("No failures occur in data (", substitute(data), "[, \"", y,
"\"])"))
}
Expand All @@ -114,7 +117,7 @@ add_duration <- function(data, y, unitID, tID, freq="month", sort=FALSE,

# Need to order by date to id and drop ongoing spells
keep <- c(y, unitID, tID, "orig_order_track")
res <- data[order(data[, unitID], data[, tID]), keep]
res <- data[order(data[[unitID]], data[[tID]]), keep]

# Mark failure (0, 1, NA for ongoing)
if (ongoing==TRUE) {
Expand Down
10 changes: 5 additions & 5 deletions R/attempt_date.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@
#' @keywords internal

attempt_date <- function(date, by) {
if (!class(date)=="Date") {
if (!inherits(date, "Date")) {
date <- as.character(date)
try(date <- as.Date(date), silent=TRUE)
if (class(date)=="Date") {
if (inherits(date, "Date")) {
warning("Converting to 'Date' class")
} else if (by=="year") {
try(date <- as.Date(paste0(date, "-06-30")), silent=TRUE)
if (class(date)=="Date") {
if (inherits(date, "Date")) {
warning("Converting to 'Date' class with yyyy-06-30")
}
} else if (by=="month") {
try(date <- as.Date(paste0(date, "-15")), silent=TRUE)
if (class(date)=="Date") {
if (inherits(date, "Date")) {
warning("Converting to 'Date' class with yyyy-mm-15")
}
}
}
if (!class(date)=="Date") {
if (!inherits(date, "Date")) {
stop(paste("Could not convert to class 'Date'"))
}
return(date)
Expand Down

0 comments on commit 0ffeaea

Please sign in to comment.