R/indmat.R

Defines functions indmat

Documented in indmat

#' Create an Indicator Matrix
#' 
#' Creates an indicator matrix from a grouping vector.
#' 
#' @param grp.vec Numeric vector giving the group membership.  
#' @param K Scalar indicating the number of groups. Defaults to the number of
#' unique elements in \code{grp.vec}.
#' @export indmat
indmat <- function(grp.vec, K = length(unique(grp.vec))){
  G <- diag(K)
  if(!is(grp.vec, 'numeric')) grp.vec <- as.numeric(grp.vec)
  G <- G[grp.vec, , drop = FALSE]
  colnames(G) <- paste("g",1:K,sep="")
  G
}

Try the cds package in your browser

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

cds documentation built on May 2, 2019, 5:54 a.m.