From 2b114285c0e9a83797aa884d574d2fa68e369770 Mon Sep 17 00:00:00 2001 From: Tini Garske Date: Thu, 21 Mar 2024 12:55:53 +0000 Subject: [PATCH] new function as_popim_vacc_activities() --- NAMESPACE | 1 + R/popim_population.R | 12 +++++++----- R/popim_vacc_activities.R | 25 +++++++++++++++++++++++-- man/as_popim_pop.Rd | 12 +++++++----- man/as_vacc_activities.Rd | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 70 insertions(+), 12 deletions(-) create mode 100644 man/as_vacc_activities.Rd diff --git a/NAMESPACE b/NAMESPACE index 77fdfd4..ae18649 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,7 @@ export(apply_vacc) export(as_popim_pop) +export(as_vacc_activities) export(calc_pop_immunity) export(complete_vacc_activities) export(plot_immunity) diff --git a/R/popim_population.R b/R/popim_population.R index abc3cab..4675aee 100644 --- a/R/popim_population.R +++ b/R/popim_population.R @@ -138,9 +138,9 @@ read_popim_pop <- function(file) { ##' columns and data ranges), and if so converts it to a ##' `popim_population` object and returns this. ##' -##' The input dataframe has to have at least the columns region, age, -##' and year. The output popim_population object is generated via -##' expand.grid to have consecutive year and age ranges that are +##' The input dataframe has to have at least the columns `region`, +##' `age`, and `year`. The output popim_population object is generated +##' via expand.grid to have consecutive year and age ranges that are ##' identical for all regions. ##' ##' If the input dataframe contains a column `pop_size`, this must be @@ -151,9 +151,11 @@ read_popim_pop <- function(file) { ##' numeric, with values between 0 and 1. If it is missing, this ##' column is generated and initialised to 0. ##' -##' Any further colunms are simply carried over into the popim_population object. +##' Any further colunms are simply carried over into the +##' popim_population object. ##' -##' @param df a dataframe with at least columns region, age, year and pop_size. +##' @param df a dataframe with at least columns region, age, year and +##' pop_size. ##' @return an object of class `popim_population` ##' @author Tini Garske ##' @export diff --git a/R/popim_vacc_activities.R b/R/popim_vacc_activities.R index 8e117f2..b369ea7 100644 --- a/R/popim_vacc_activities.R +++ b/R/popim_vacc_activities.R @@ -173,10 +173,31 @@ read_vacc_activities <- function(file) { stop(sprintf("%s does not exist", file), call. = FALSE) df <- utils::read.csv(file, stringsAsFactors = FALSE) - class(df) <- c("popim_vacc_activities", "data.frame") + df <- as_vacc_activities(df) + df +} - validate_vacc_activities(df) +##' Generate a `popim_vacc_activites object from a dataframe +##' +##' Checks if the dataframe is suitable (i.e., contains appropriate +##' columns and data ranges), and if so converts it to a +##' `popim_vacc_activities` object and returns this. +##' +##' The input dataframe has to have at least teh columns `region`, +##' `year`, `age_first`, `age_last`, `coverage`, `doses` and +##' `targeting`. Any further columns are simply carried over into the +##' `popim_vacc_activities` object. +##' +##' @param df dataframe to be converted to class +##' `popim_vacc_activities` +##' @return an object of class `popim_vacc_activities` +##' @author Tini Garske +##' @seealso [popim_vacc_activities()] for details of the S3 class. +##' @export +as_vacc_activities <- function(df) { + class(df) <- c("popim_vacc_activities", "data.frame") + validate_vacc_activities(df) df } diff --git a/man/as_popim_pop.Rd b/man/as_popim_pop.Rd index 7029c9e..52475d2 100644 --- a/man/as_popim_pop.Rd +++ b/man/as_popim_pop.Rd @@ -7,7 +7,8 @@ as_popim_pop(df) } \arguments{ -\item{df}{a dataframe with at least columns region, age, year and pop_size.} +\item{df}{a dataframe with at least columns region, age, year and +pop_size.} } \value{ an object of class \code{popim_population} @@ -18,9 +19,9 @@ columns and data ranges), and if so converts it to a \code{popim_population} object and returns this. } \details{ -The input dataframe has to have at least the columns region, age, -and year. The output popim_population object is generated via -expand.grid to have consecutive year and age ranges that are +The input dataframe has to have at least the columns \code{region}, +\code{age}, and \code{year}. The output popim_population object is generated +via expand.grid to have consecutive year and age ranges that are identical for all regions. If the input dataframe contains a column \code{pop_size}, this must be @@ -31,7 +32,8 @@ If the input dataframe contains a colum \code{immunity}, this must be numeric, with values between 0 and 1. If it is missing, this column is generated and initialised to 0. -Any further colunms are simply carried over into the popim_population object. +Any further colunms are simply carried over into the +popim_population object. } \author{ Tini Garske diff --git a/man/as_vacc_activities.Rd b/man/as_vacc_activities.Rd new file mode 100644 index 0000000..2d6515e --- /dev/null +++ b/man/as_vacc_activities.Rd @@ -0,0 +1,32 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/popim_vacc_activities.R +\name{as_vacc_activities} +\alias{as_vacc_activities} +\title{Generate a `popim_vacc_activites object from a dataframe} +\usage{ +as_vacc_activities(df) +} +\arguments{ +\item{df}{dataframe to be converted to class +\code{popim_vacc_activities}} +} +\value{ +an object of class \code{popim_vacc_activities} +} +\description{ +Checks if the dataframe is suitable (i.e., contains appropriate +columns and data ranges), and if so converts it to a +\code{popim_vacc_activities} object and returns this. +} +\details{ +The input dataframe has to have at least teh columns \code{region}, +\code{year}, \code{age_first}, \code{age_last}, \code{coverage}, \code{doses} and +\code{targeting}. Any further columns are simply carried over into the +\code{popim_vacc_activities} object. +} +\seealso{ +\code{\link[=popim_vacc_activities]{popim_vacc_activities()}} for details of the S3 class. +} +\author{ +Tini Garske +}