% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/sort_missing_data.R
\name{sort_missing_data}
\alias{sort_missing_data}
\title{Sort Matrix/Data Frame According to the Pattern of Missing Elements}
\usage{
sort_missing_data(X, misslast = 1, nobs = 0)
}
\arguments{
\item{X}{a data frame or matrix to be sorted.}

\item{misslast}{= 1 to treat NA as the largest,
= 1 to treat NA as the smallest.}

\item{nobs}{= 1 to sort by the # of observed elements first \cr
= -1 to sort by the descending # of observed elements first \cr
= 0 not to pay any attention to the # of missing elements.}
}
\value{
A list of (sorted_data, freq, Nobs, class, od) \cr
where sorted_data is of the same size as X, \cr
freq is the vector consisting of the # of observatons for each pattern, \cr
Nobs is the vector of length nrow(X)
containing the # of observed elements for each row of X.\cr
class is the interger in [1,length(freq)] indicating the classification
of each obs according to the missing pattern. \cr
od is the order to be used to sort X as X_sorted=X[od,].
}
\description{
Sort Matrix/Data Frame According to the Pattern of Missing Elements
}
\examples{
set.seed(17029)
nn=50
dataframe=0

if( dataframe ){
 x=sample(0:9,nn,replace=1)
 x[runif(nn) < 0.5]=NA
 x2=sample(letters[1:10],nn,replace=1)
 x2[runif(nn) < 0.5]=NA
 x3=sample(LETTERS[1:10],nn,replace=1)
 x3[runif(nn) < 0.5]=NA
 X=data.frame(x,x2,x3, stringsAsFactors=0)
} else{
 x=sample(0:9,nn,replace=1)
 x[runif(nn) < 0.3]=NA
 x2=sample(1:10,nn,replace=1)
 x2[runif(nn) < 0.3]=NA
 x3=sample(1:10,nn,replace=1)
 x3[runif(nn) < 0.3]=NA
 X=cbind(x,x2,x3)
}
res=sort_missing_data( X, misslast=1, nobs=-1 )
Xs=res$sorted_data
freqs=res$freq
Nobss=res$Nobs
od=res$od

# Nobsss should be equal to Nobs, Xss, to Xs
Nobsss=ncol(X)-apply(Xs,1,function(x) sum(is.na(x)))
Xss=X[od,]
pat=rep(1:length(freqs),freqs)
Print(Xs,Xss,Nobss, pat,od)

misspat=Xs[cumsum(res$freq),]
nobs=ncol(misspat)-apply(misspat,1,function(x) sum(is.na(x)))
Print(misspat,nobs)

mands( Xs, by=pat )


}
