jranke / mkin
1
utils::globalVariables(c("name", "time", "value"))
2

3
#' Convert a dataframe with observations over time into long format
4
#' 
5
#' This function simply takes a dataframe with one independent variable and
6
#' several dependent variable and converts it into the long form as required by
7
#' \code{\link{mkinfit}}.
8
#' 
9
#' @param wide_data The dataframe must contain one variable with the time
10
#'   values specified by the \code{time} argument and usually more than one
11
#'   column of observed values.
12
#' @param time The name of the time variable.
13
#' @return Dataframe in long format as needed for \code{\link{mkinfit}}.
14
#' @author Johannes Ranke
15
#' @keywords manip
16
#' @examples
17
#' 
18
#' wide <- data.frame(t = c(1,2,3), x = c(1,4,7), y = c(3,4,5))
19
#' mkin_wide_to_long(wide)
20
#' 
21
#' @export
22
mkin_wide_to_long <- function(wide_data, time = "t")
23
{
24 1
  wide_data <- as.data.frame(wide_data)
25 1
  colnames <- names(wide_data)
26 0
  if (!(time %in% colnames)) stop("The data in wide format have to contain a variable named ", time, ".")
27 1
  vars <- subset(colnames, colnames != time)
28 1
  n <- length(colnames) - 1
29 1
  long_data <- data.frame(
30 1
    name = rep(vars, each = length(wide_data[[time]])),
31 1
    time = as.numeric(rep(wide_data[[time]], n)),
32 1
    value = as.numeric(unlist(wide_data[vars])),
33 1
    row.names = NULL)
34 1
  return(long_data)
35
}

Read our documentation on viewing source code .

Loading