[18884] in s-news-athena

home help back first fref pref prev next nref lref last post

[S] "runs" of missing data in a vector

daemon@ATHENA.MIT.EDU (Barker, Chris {PHAR~Palo Alto})
Fri Aug 6 18:56:39 1999

Content-Return: allowed
Date: Fri, 06 Aug 1999 15:51:13 -0700
From: "Barker, Chris {PHAR~Palo Alto}" <CHRIS.BARKER@ROCHE.COM>
To: "'s-news@wubios.wustl.edu'" <s-news@wubios.wustl.edu>
Message-Id: <B9E51B9719BDD111AB640000F80197CD0164635C@rplmsem2.pal.roche.com>
Mime-Version: 1.0
Content-Type: text/plain; CHARSET=US-ASCII


(My apology in advance for the message length - I attached a short function)

I have a large dataframe with 600 patients, each patient measured daily for
21 days. 
I want to identify patterns of missing data so I can prepare groupedData
plots according to pattern of missing data.

I developed a small function (below) which helps me identify
patterns or "runs" of missing data. I use tapply and the 
function returns a list with an entry for each patient with 
a frequency table of the "runs".  
I'd like to convert this list to a data frame which holds
 the patient number and the frequency of each "run" for that patient.

(This issue has been discussed in recent posts on this news server, but I
haven't had luck in using the methods discussed, e.g.  "expand.grid")

Any suggestions appreciated.

-------function follows----------------------
To clarify, in test vector below, there are 3 "runs" of NA, two that last a
day and a third for 2 days.

test <- c(11,NA,12,NA,18,NA,NA)

I invoke the function using tapply
pt.run.count <- tapply( response.var ,patient.number,FUN=missing.data.runs)

(I'd like to convert pt.run.count to a dataframe and retain the patient
number)

missing.data.runs <- function(vector.test){
zz<-NA;runs<-0  ;	runs.vector <-c(rep(NA,length(vector.test)))
if ( !is.na(max(test,na.rm=T) ) )  {
	for (i in 1:(length(vector.test)))
	{ 
		if (i==1){runs<-0}		
		if (!is.na(vector.test[i])) {
			runs<-0
			runs.vector[i] <-NA
			}
	if ( is.na(vector.test[i]) ) {
	runs<-runs+1
	runs.vector[i]<-runs
	if ( i>1) {if (is.na(vector.test[i-1])){ runs.vector[i-1] <-NA }}  }
}
	 } 
else runs.vector <- NA	
if ( !is.na(max(runs.vector,na.rm=T)) ) { zz <- table(runs.vector) }
	else zz<-c(length(vector.test))
if ( 	max( !is.na(vector.test) )==1  ) {runs.vector	}
if ( !is.na(max(vector.test)) ) { zz <- 0 }
return(zz)
}

Chris Barker, Ph.D.
Principal Pharmacoeconomic Statistician
Roche Pharma Business - Palo Alto
(650)-852-3152

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

home help back first fref pref prev next nref lref last post