Skip to content
Prev 164361 / 398503 Next

How do I multiply labeled vectors of numbers?

on 12/10/2008 05:17 PM Bill McNeill (UW) wrote:
F3 <- merge(F1, F2, by = "type", all = TRUE)
type n.x n.y
1   cat   4   9
2   dog   2   3
3 horse   6   1
4 mouse  NA  11


# Convert NA's to 0

F3[is.na(F3)] <- 0
type n.x n.y
1   cat   4   9
2   dog   2   3
3 horse   6   1
4 mouse   0  11


F3$prod <- F3$n.x * F3$n.y
type n.x n.y prod
1   cat   4   9   36
2   dog   2   3    6
3 horse   6   1    6
4 mouse   0  11    0


See ?merge

HTH,

Marc Schwartz