[R] counting missing values

Hotz, T. th50 at leicester.ac.uk
Thu Jun 5 16:37:52 CEST 2003


Dear Vincent

> I'd like to know if there is a function already implemented 
> to count the
> number of occurence of a given values in a vector

Does

my.matrix<-cbind(c(0,1,NA),c(NA,1,NA))
count.NA<-function(the.matrix){
  result<-t(apply(the.matrix,1,function(x,n){
    m<-sum(is.na(x))
    c(m,m/n)
  },n=dim(the.matrix)[2]))
  dimnames(result)<-list(dimnames(the.matrix)[[1]],c("row count","proportion"))
  result
}
count.NA(my.matrix)

do what you were looking for? Hope that helps.

Cheers

Thomas

---

Thomas Hotz
Research Associate in Medical Statistics
University of Leicester
United Kingdom

Department of Epidemiology and Public Health
22-28 Princess Road West
Leicester
LE1 6TP
Tel +44 116 252-5410
Fax +44 116 252-5423

Division of Medicine for the Elderly
Department of Medicine
The Glenfield Hospital
Leicester
LE3 9QP
Tel +44 116 256-3643
Fax +44 116 232-2976




More information about the R-help mailing list