From 0a3eb0893cb4bd1b12f07a79069d1c7f5e991495 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Fri, 25 Oct 2019 00:37:42 +0200 Subject: Use roxygen for functions and methods --- R/mkin_wide_to_long.R | 68 +++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 34 deletions(-) (limited to 'R/mkin_wide_to_long.R') diff --git a/R/mkin_wide_to_long.R b/R/mkin_wide_to_long.R index 21a7cbe9..bef0e408 100644 --- a/R/mkin_wide_to_long.R +++ b/R/mkin_wide_to_long.R @@ -1,34 +1,34 @@ -# $Id$ - -# Copyright (C) 2010-2013 Johannes Ranke -# Contact: mkin-devel@lists.berlios.de - -# This file is part of the R package mkin - -# mkin is free software: you can redistribute it and/or modify it under the -# terms of the GNU General Public License as published by the Free Software -# Foundation, either version 3 of the License, or (at your option) any later -# version. - -# This program is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more -# details. - -# You should have received a copy of the GNU General Public License along with -# this program. If not, see -if(getRversion() >= '2.15.1') utils::globalVariables(c("name", "time", "value")) - -mkin_wide_to_long <- function(wide_data, time = "t") -{ - colnames <- names(wide_data) - if (!(time %in% colnames)) stop("The data in wide format have to contain a variable named ", time, ".") - vars <- subset(colnames, colnames != time) - n <- length(colnames) - 1 - long_data <- data.frame( - name = rep(vars, each = length(wide_data[[time]])), - time = as.numeric(rep(wide_data[[time]], n)), - value = as.numeric(unlist(wide_data[vars])), - row.names = NULL) - return(long_data) -} +if(getRversion() >= '2.15.1') utils::globalVariables(c("name", "time", "value")) + +#' Convert a dataframe with observations over time into long format +#' +#' This function simply takes a dataframe with one independent variable and +#' several dependent variable and converts it into the long form as required by +#' \code{\link{mkinfit}}. +#' +#' @param wide_data The dataframe must contain one variable with the time +#' values specified by the \code{time} argument and usually more than one +#' column of observed values. +#' @param time The name of the time variable. +#' @return Dataframe in long format as needed for \code{\link{mkinfit}}. +#' @author Johannes Ranke +#' @keywords manip +#' @examples +#' +#' wide <- data.frame(t = c(1,2,3), x = c(1,4,7), y = c(3,4,5)) +#' mkin_wide_to_long(wide) +#' +#' @export +mkin_wide_to_long <- function(wide_data, time = "t") +{ + colnames <- names(wide_data) + if (!(time %in% colnames)) stop("The data in wide format have to contain a variable named ", time, ".") + vars <- subset(colnames, colnames != time) + n <- length(colnames) - 1 + long_data <- data.frame( + name = rep(vars, each = length(wide_data[[time]])), + time = as.numeric(rep(wide_data[[time]], n)), + value = as.numeric(unlist(wide_data[vars])), + row.names = NULL) + return(long_data) +} -- cgit v1.2.1