R/sparse.mat.R

Defines functions sparse.mat

Documented in sparse.mat

sparse.mat <- function(datalist){
  #
  #Create a matrix of sparse data with NAs out of a list of sparse data
  #
  # Arguments:
  #
  # DATALIST .... A list object. Each element of the list is a matrix with ncol > 1. 
  #               The first column of each element corresponds to the point index per 
  #               observation. The following columns are the observations per variable.
  
  time = sort(unique(unlist(lapply(datalist, function(x) x[,1]))))
  data = matrix(NA, nrow = length(time), ncol = length(datalist))
  
  nvar = ncol(datalist[[1]]) - 1
  
  if(nvar == 1){
    data = matrix(NA, nrow = length(time), ncol = length(datalist))
    for(i in 1:length(datalist)){
      data[which((time %in% datalist[[i]][,1])),i] = datalist[[i]][,2]
    }
  }else{
    data = array(NA, dim = c(length(time),length(datalist),nvar))
    for(j in 1:nvar){
      for(i in 1:length(datalist)){
        data[which((time %in% datalist[[i]][,1])),i,j] = datalist[[i]][,2]
      }
    }
  }
  return(data)
}

Try the fda package in your browser

Any scripts or data that you put into this service are public.

fda documentation built on Sept. 30, 2024, 9:19 a.m.