Question: This is a really simple question, but I am hoping someone will be able to help me avoid extra lines of unnecessary code. I have a simple dataframe:

```
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)))

```

What I want to do is produce an extra column which is the multiplication of A, B and C, which I will then cbind to the original dataframe.

So, I would normally use:

```
attach(Df.1)
D<-A*B*C

```

But obviously where the NAs are in column C, I get an NA in variable D. I don't want to exclude all the NA rows, rather just ignore the NA values in this column (and then the value in D would simply be the multiplication of A and B, or where C was available, A\*B\*C. 

I know I could simply replace the NAs with 1s, so the calculation remains unchanged, or use if statements, but I was wodnering what the simplist way of doing this is? 

Any ideas?

Answer: 
You can use `prod` which has an `na.rm` argument. To do it by row use `apply`:

```
apply(Df.1,1,prod,na.rm=TRUE)
[1]  10  60  14 120  72  36

```
As @James said, prod and apply will work, but you don't need to waste memory storing it in a separate variable, or even cbinding it

```
Df.1$D = apply(Df.1, 1, prod, na.rm=T)

```

Assigning the new variable in the data frame directly will work.

```
> 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  36

```