sort_missing_data {lazy.tools}R Documentation

Sort Matrix/Data Frame According to the Pattern of Missing Elements

Description

Sort Matrix/Data Frame According to the Pattern of Missing Elements

Usage

sort_missing_data(X, misslast = 1, nobs = 0)

Arguments

X

a data frame or matrix to be sorted.

misslast

= 0 to treat NA as the largest, = 1 to treat NA as the smallest.

nobs

= 1 to sort by the # of observed elements first
= -1 to sort by the descending # of observed elements first
= 0 not to pay any attention to the # of missing elements.

Value

A list of (sorted_data, freq, Nobs)
where sorted_data is of the same size as X,
freq is the vector consisting of the # of observatons for each pattern,
Nobs is the vector of length nrow(X), containing the # of observed elements for each row of X.
od is the order to be used to sort X as X_sorted=X[od,].

References

The idea of representing the missing patterns by numbers is found in
"mysort" function of "mvnmle" package.

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
nobs=ncol(X)-apply(Xs,1,function(x) sum(is.na(x)))
Xss=X[od,]
Print(Xs,Xss,Nobss,nobs,od)


[Package lazy.tools version 0.1.3 Index]