I am adding two new columns and for some reason, calculation works on one column but not on other column. For this second column which is not working, the result is always NA. I do have some NAs in two of the columns used for its calculation. The sample here shows all NAs, but in real data, its a combination of values and NAs. I also tried replacing NA with 0 and still I see NA as result. In the below example, NP_O should give value of NP when NP_C and NP_T are NA, but it doesn't work for some reason.
Can you please help? Thank you!
df <- data.frame( stringsAsFactors = FALSE, Date = c("2015/01/01", "2015/01/01","2015/01/01", "2015/01/01","2015/02/01"), Country = c("Brazil", "Canada","Mexico","USA","Brazil"), M = c(510875,95736, 319243,1205822.64843412,457566), NP = c(82280,19147.2, 180710,217251,74068), NP_C = c(NA, NA, NA, NA, NA), NP_T = c(NA, NA, NA, NA, NA) ) # Calculating P and NP_O df <- df %>% mutate("P" = M - `NP`)%>% mutate("NP_O" = `NP` - `NP_C` - `NP_T`)