[R] Alternative to "apply" in R 3.2.2

Mark Sharp msharp at txbiomed.org
Fri Nov 11 22:08:07 CET 2016


Olu,

I think you may have misread what na.rm is supposed to do. I think you are getting the correct value. If you want the vectors that contain NA values to be evaluated to NA then you will need to set na.rm to FALSE (which is the default for prod()).

prod(c(7, 2, NA), na.rm = TRUE)
[1] 14
prod(c(7, 2, NA), na.rm = FALSE)
[1] NA

Mark
R. Mark Sharp, Ph.D.
Director of Data Science Core
Southwest National Primate Research Center
Texas Biomedical Research Institute
P.O. Box 760549
San Antonio, TX 78245-0549
Telephone: (210)258-9476
e-mail: msharp at TxBiomed.org









> On Nov 11, 2016, at 2:45 PM, Olu Ola via R-help <r-help at r-project.org> wrote:
>
> Hello,I have a dataset that is similar to the one as follows:
>
>> Df.1 <- data.frame(A = c(5,4,7,6,8,4),B = (c(1,5,2,4,9,1)),C=(c(2,3,NA,5,NA,9)))
>> Df.1
>  A B  C
> 1 5 1  2
> 2 4 5  3
> 3 7 2 NA
> 4 6 4  5
> 5 8 9 NA
> 6 4 1  9
>> Df.1$D = apply(Df.1, 1, prod, na.rm=T)
>> Df.1$D[1]  10  60  14 120  72  36
>> Df.1
>  A B  C   D
> 1 5 1  2  10
> 2 4 5  3  60
> 3 7 2 NA  14
> 4 6 4  5 120
> 5 8 9 NA  72
> 6 4 1  9  36I intend to obtain a column D that takes into account na.rm=T but 'apply' does not work in R 3.2.2
>
> A way forward will be greatly appreciated.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

CONFIDENTIALITY NOTICE: This e-mail and any files and/or...{{dropped:10}}



More information about the R-help mailing list