diff --git a/DESCRIPTION b/DESCRIPTION index aacc796..e4842f3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -3,13 +3,13 @@ Type: Package Title: The Critical Care Clinical Data Processing Tools Version: 0.1 Date: 2017-01-30 -Author: Sinan Shi, David Pérez-Suárez, Steve Harris, Niall MacCallum, David Brealey, Mervyn Singer, James Hetherington +Author: Sinan Shi, David Pérez-Suárez, Steve Harris, Niall MacCallum, David + Brealey, Mervyn Singer, James Hetherington Maintainer: Sinan Shi -Description: - A toolset to deal with the Critical Care Health Informatics Collaborative - dataset. It is created to address various data reliability and accessibility - problems of electronic healthcare records (EHR). It provides a unique - platform which enables data manipulation, transformation, reduction, +Description: A toolset to deal with the Critical Care Health Informatics + Collaborative dataset. It is created to address various data reliability and + accessibility problems of electronic healthcare records (EHR). It provides a + unique platform which enables data manipulation, transformation, reduction, anonymisation, cleaning and validation. Depends: R (>= 3.1.0) @@ -25,7 +25,7 @@ Imports: methods, knitr, ggplot2, - pander, + pander, stats, utils VignetteBuilder: knitr @@ -38,6 +38,7 @@ Collate: 'create2dclean.R' 'data.quality.report.R' 'deltaTime.R' + 'demographics.R' 'filter.categorical.R' 'filter.missingness.R' 'filter.range.R' diff --git a/NAMESPACE b/NAMESPACE index bd53eeb..6297e78 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -25,6 +25,7 @@ export(is.demographic) export(is.drugs) export(is.laboratory) export(is.physiology) +export(lenstay) export(lookup.items) export(new.episode) export(physio.distribution) diff --git a/R/demographics.R b/R/demographics.R new file mode 100644 index 0000000..7b15fd4 --- /dev/null +++ b/R/demographics.R @@ -0,0 +1,16 @@ +#' Calculate the lenght of stay in the ICU. +#' +#' Calculate the lenght of stay in the ICU and append it to the original demographic +#' table. +#' @param demg data.table the demograhic table which should at least contain +#' column DAICU and DDICU +#' @param units character The unit of lenstay column, by default the output will be in hours +#' @return data.table It is the original data.table with lenstay column (in +#' difftime) appended. +#' @export lenstay +lenstay <- function(demg, units="hours") { + len <- difftime(xmlTime2POSIX(demg$DDICU, allow=T), + xmlTime2POSIX(demg$DAICU, allow=T), + units=units) + return(cbind(demg, lenstay = len)) +} diff --git a/man/lenstay.Rd b/man/lenstay.Rd new file mode 100644 index 0000000..347bdc2 --- /dev/null +++ b/man/lenstay.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/demographics.R +\name{lenstay} +\alias{lenstay} +\title{Calculate the lenght of stay in the ICU.} +\usage{ +lenstay(demg, units = "hours") +} +\arguments{ +\item{demg}{data.table the demograhic table which should at least contain +column DAICU and DDICU} + +\item{units}{character The unit of lenstay column, by default the output will be in hours} +} +\value{ +data.table It is the original data.table with lenstay column (in +difftime) appended. +} +\description{ +Calculate the lenght of stay in the ICU and append it to the original demographic +table. +} + diff --git a/tests/testthat/test_demographics.r b/tests/testthat/test_demographics.r new file mode 100644 index 0000000..ecda9aa --- /dev/null +++ b/tests/testthat/test_demographics.r @@ -0,0 +1,26 @@ +context("Testing functionalities for the demographic table") + +test_that("calculate length of stay in the ICU",{ + demg <- data.table(DAICU="2007-01-01", DDICU="2007-01-02") + demg_ <- lenstay(demg) + expect_true(is.data.frame(demg_)) + expect_equal(ncol(demg) + 1, ncol(demg_)) + expect_equal(as.numeric(demg_$lenstay), 24) + + demg <- data.table(DAICU="2007-01-01T00:00:00", DDICU="2007-01-01T22:00:00") + demg_ <- lenstay(demg) + expect_equal(as.numeric(demg_$lenstay), 22) + + demg <- data.table(DAICU="2007-01-01", DDICU="wrong_format") + demg_ <- lenstay(demg) + expect_true(is.na(demg_$lenstay)) + + demg <- data.table(DAICU="wrong_format1", DDICU="wrong_format1") + demg_ <- lenstay(demg) + expect_true(is.na(demg_$lenstay)) + + demg <- data.table(DAICU="2007-01-01T00:00:00", DDICU="2007-01-01T22:00:00") + demg_ <- lenstay(demg, "days") + expect_equal(as.numeric(demg_$lenstay), 1-2/24) +}) +