[R] Need to incorporate the use of na.rm into custom function

Ochsner, Scott A sochsner at bcm.edu
Fri Oct 1 17:07:02 CEST 2010


Hi,

Take a matrix with missing values:

> X = matrix(rnorm(10), ncol = 5)
> X[2,4]=NA
> X
           [,1]       [,2]       [,3]       [,4]       [,5]
[1,] -0.1566427 -0.7382232 -1.0564624 -0.8412139  0.9370319
[2,] -1.0289865 -0.8452054 -0.1349459         NA -0.1749113

I want to apply a custom function over the rows such that the NAs are ignored in a similar fashion as to how the following works.

> means<-apply(X,1,mean,na.rm=TRUE)

Custom function:

liptak<-function (x,df) 2*(pnorm(abs(sum(x*df)/sqrt(sum(df^2))),lower.tail=FALSE))

I want to be able to do the following:

>rslt<-apply(X,1,liptak,na.rm=TRUE)

Can someone point me in the right direction on how to incorporate the use of na.rm into my function?


Scott

> sessionInfo()
R version 2.11.0 (2010-04-22) 
i386-pc-mingw32 

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] tools_2.11.0

Scott A. Ochsner, PhD
NURSA Bioinformatics
Baylor College of Medicine
One Baylor Plaza
Mail Stop: BCM-130
Houston, TX 77030
Voice: (713) 798-6227
Fax: (713) 790-1275


More information about the R-help mailing list