I have a data frame in the form shown below:
structure(list(First = c("3.2", "3.2", "3.2", NA, "3.3", NA,
NA), `cut-off` = c("100000", "100000", "100000", NA, "100000",
NA, NA), Second = c("1.1", "1.2", "1.2", NA, "1.2", "1.6", NA
), Bonus = c(NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_), Fixed = c(NA, NA,
NA, "4000", NA, NA, NA)), row.names = c(NA, -7L), class = c("tbl_df",
"tbl", "data.frame"))
# A tibble: 6 x 5
First `cut-off` Second Bonus Fixed
<chr> <chr> <chr> <chr> <chr>
1 3.2 100000 1.1 NA NA
2 3.2 100000 1.2 NA NA
3 3.2 100000 1.2 NA NA
4 NA NA NA NA 4000
5 3.3 100000 1.2 NA NA
6 NA NA 1.6 NA NA
7 NA NA NA NA NA
What I want to do is calculating a new column called Total which will be calculated from the equation shown below:
Total=(First*cut-off/100)+(second*300000/100)+Bonus+Fixed
The problem I have is that based on the situation, a few cells in each row will be NA but I want to ignore NA and considering it as 0 in mathematical calculation (but I can't replace it with 0 as for some cases when all cells in a row are NA, I want to show NA in Total column). So the expeted result for the above dataframe would be:
First `cut-off` Second Bonus Fixed Total
<chr> <chr> <chr> <chr> <chr>
1 3.2 100000 1.1 NA NA 6500
2 3.2 100000 1.2 NA NA 6800
3 3.2 100000 1.2 NA NA 6800
4 NA NA NA NA 4000 4000
5 3.3 100000 1.2 NA NA 6900
6 NA NA 1.6 NA NA 4800
7 NA NA NA NA NA NA
How can I do this?