R/fc.wide2long.R

Defines functions fc.wide2long

#' Go through all observations in the matrix and list them
#' in a 'long' style
#' @noRd
# @export
fc.wide2long = function(X, time = NULL, ids = NULL, value = "value")
{
  # TODO: This function should be optimized

  if (is.null(time))
    time = as.numeric(colnames(X))
  if (is.null(ids))
    ids = rownames(X)
  long = data.frame(id = c(), time = c(), value=c())

  for (i in 1:length(ids)){
    observed = X[i,!is.na(X[i,])]
    id = rep(ids[i], length(observed))
    tm = time[!is.na(X[i,])]
    toadd = data.frame(id=id,
                       time=tm,
                       value=observed)
    long = rbind(long,toadd)
  }
  rownames(long) = NULL
  colnames(long)[3] = value
  long
}
kidzik/fcomplete documentation built on Aug. 24, 2023, 5:44 a.m.