On 15/11/16 09:52, Olu Ola via R-help wrote:> Hello,I have a data set called plabor and have the following format:
>
> | ColA | ColB | Colc |
> | 6 | 25 | 3 |
> | NA | NA | NA |
> | 3 | 2 | 19 |
> | 4 | 7 | NA |
>
>
> I wanted to find the product of the three columns for each of the rows and
I used the apply function follows:
> plabor$colD =
apply(plabor[c("colA","colB","colc")],1,prod,na.rm=T)
> The result are as follows:
>
> | ColA | ColB | Colc | colD |
> | 6 | 25 | 3 | 450 |
> | NA | NA | NA | 1 |
> | 3 | 2 | 19 | 114 |
> | 4 | 7 | NA | 28 |
>
>
> The second row results is 1 instead of being ignored.
> How do I deal with this issue because I do not want to exclude these data
points with all NA's?
What do you mean by "ignored"? If you really want rows of your matrix
that are all NA to be omitted from consideration, delete such rows from
your matrix a priori.
If you want the product of such rows to be NA rather than 1, use a
"customised" function rather than prod() in your apply, with an
appropriate if-else construction in the customised function.
It's very easy, and I won't tell you the details because I think
it's
time you actually learned something about R (given that you are using
R), and one learns by doing.
cheers,
Rolf Turner
--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276