R/getDN.means.R

Defines functions getDN.means

Documented in getDN.means

# This function finds the datanugget mean of a given datanugget object
# It returns the dataframe of the datanugget means.

# Function Inputs:
# x: original dataset.
# DN: a datanugget object.


getDN.means <- function(x, DN){
  
  
  
  ## -------------------------------
  ## Argument checks
  ## -------------------------------
  
  # check x
  if (!any(class(x) %in% c("matrix", "data.frame", "data.table"))){
    stop('x must be of class "matrix", "data.frame", or "data.table"')
  }
  
  
  # check DN
  if (!inherits(DN,"datanugget")){
    stop('DN must be of class "datanugget"')
  }
  
  
  ## ----------------------------------
  ## Pre processing and Initialization
  ## ----------------------------------
  
  
  # convert the input to matrix
  x_mat <- as.matrix(x)
  
  
  # number of data nuggets
  n.dns <- nrow(DN$"Data Nuggets")
  
  
  # number of features (excluding datanugget number, scale and weight)
  n.features <- ncol(DN$"Data Nuggets") - 3
  
  
  # datanugget centers
  DN.centers <- DN$"Data Nuggets"[, 2:(n.features + 1)]
  
  
  # datanugget assignments
  DN.assignments <- DN$"Data Nugget Assignments"
  
  
  ## ----------------------
  ## Working functions
  ## ----------------------
  
  
  out <- t(sapply(1:n.dns, function(i){
    
    
    index_vector <- which(DN.assignments == i)
    
    
    obs.dn <- x_mat[index_vector, , drop = FALSE]
    
    
    center <- colMeans(obs.dn)
  }))
  
  colnames(out) <- colnames(DN.centers)
  
  out <- as.data.frame(out)
  
  return(out)
  
}

Try the datanugget package in your browser

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

datanugget documentation built on Aug. 21, 2026, 9:10 a.m.