title: How to fill NA's when you have enough information to calculate what the NA should be in R
body: <p>I've got a dataset where there are some NA's but I can manually work out what the values should be as the df is a column for name and the rest of the columns are just numbers followed by a final column with total. Only one NA appears per row at most so I can work out what the value should be by using the total column and the sum of all the other columns. Just wondering what would be the best way to fill these NA's without having to hardcode one by one as the df I'm using is pretty big</p>
  <p>example df:</p>
  <pre><code>df = structure(list(city = c(&quot;sydney&quot;, &quot;new york&quot;, &quot;london&quot;, &quot;beijing&quot;, &quot;paris&quot;, &quot;madrid&quot;), 
                      year = c(2005:2010), 
                      A = c(1, 4, 5 , NA, 2, 1), 
                      B = c(3, NA, 4 , 9, 0, 6),
                      C = c(3, 4 , 6, 1, 8, NA),
                      total = c(NA, 10, 15, 14, NA, 15)), 
                 class = &quot;data.frame&quot;, row.names = c(NA, -6L))
  
  df
  </code></pre>