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

Jeffrey Spies jspies at virginia.edu
Fri Oct 1 17:12:56 CEST 2010


You can use na.omit on x after it is passed into your apply function
if na.rm == T, or simply pass your na.rm to functions that use it,
such as sum.

Hope that helps,

Jeff.

On Fri, Oct 1, 2010 at 11:07 AM, Ochsner, Scott A <sochsner at bcm.edu> wrote:
> 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
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list