title: Replace NA's in some columns and rows based on a lookup table in R
body: <p>Replicable dataset below.</p>
  <p>Let me set the scene: I have a detection history of 3 species recorded by 5 cameras on concurrent days (V1:V10)(1=detected, 0=undetected).
  Cameras 1 and 2 did not record on the first and last days, hence the NAs in columns V1 and V10.
  Camera 5 did not record for the first and last two days, hence these NAs.</p>
  <p>Because no caracals were recorded by camera 2, and no leopards were recorded by camera 5, these rows are fully populated with NAs.</p>
  <p>I know, however, that cameras 2 and 5 did work during this time so there should be &quot;0&quot;s for V2:V9 for caracals at camera 2 and for V3:V8 for leopards at camera 5.</p>
  <p>So my question is: how can I populate these cells with 0s rather than NAs?</p>
  <p>I need to maintain NAs when the cameras were not working during the first and last days.</p>
  <p>My actual dataset has hundreds more species, cameras, and days so I cannot do this manually.</p>
  <p>I have been trying to figure this out but have had absolutely no success.
  I thought about creating a lookup table of when each camera worked but couldn't figure out the syntax.</p>
  <p>Any help is appreciated</p>
  <pre><code>camera&lt;-c(rep(1:5,each=3))
  species_names&lt;-c(&quot;baboon&quot;,&quot;caracal&quot;, &quot;leopard&quot;)
  species&lt;-c(rep(species_names,5))
  V1&lt;-c(NA,NA,NA,NA,NA,NA,0,0,1,1,1,0,NA,NA,NA)
  V2&lt;-c(0,1,0,0,NA,0,0,0,1,0,0,1,NA,NA,NA)
  V3&lt;-c(1,0,1,0,NA,0,1,1,0,1,0,0,1,0,NA)
  V4&lt;-c(0,1,1,1,NA,0,0,0,0,1,1,0,0,0,NA)
  V5&lt;-c(1,1,1,0,NA,1,1,1,0,0,1,1,1,0,NA)
  V6&lt;-c(1,0,1,1,NA,1,1,1,1,0,0,1,1,1,NA)
  V7&lt;-c(0,0,1,0,NA,0,0,1,0,0,0,0,1,0,NA)
  V8&lt;-c(1,1,1,1,NA,1,0,0,0,0,1,0,0,0,NA)
  V9&lt;-c(0,0,1,0,NA,0,0,1,1,1,1,0,NA,NA,NA)
  V10&lt;-c(NA,NA,NA,NA,NA,NA,0,1,0,0,0,0,NA,NA,NA)
  
  dataset&lt;-data.frame(camera,species,V1,V2,V3,V4,V5,V6,V7,V8,V9,V10)
  dataset
  
     camera species V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
  1       1  baboon NA  0  1  0  1  1  0  1  0  NA
  2       1 caracal NA  1  0  1  1  0  0  1  0  NA
  3       1 leopard NA  0  1  1  1  1  1  1  1  NA
  4       2  baboon NA  0  0  1  0  1  0  1  0  NA
  5       2 caracal NA NA NA NA NA NA NA NA NA  NA
  6       2 leopard NA  0  0  0  1  1  0  1  0  NA
  7       3  baboon  0  0  1  0  1  1  0  0  0   0
  8       3 caracal  0  0  1  0  1  1  1  0  1   1
  9       3 leopard  1  1  0  0  0  1  0  0  1   0
  10      4  baboon  1  0  1  1  0  0  0  0  1   0
  11      4 caracal  1  0  0  1  1  0  0  1  1   0
  12      4 leopard  0  1  0  0  1  1  0  0  0   0
  13      5  baboon NA NA  1  0  1  1  1  0 NA  NA
  14      5 caracal NA NA  0  0  0  1  0  0 NA  NA
  15      5 leopard NA NA NA NA NA NA NA NA NA  NA
  
  
  </code></pre>
  <p>My dataset should look like this:</p>
  <pre><code>   camera species V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
  1       1  baboon NA  0  1  0  1  1  0  1  0  NA
  2       1 caracal NA  1  0  1  1  0  0  1  0  NA
  3       1 leopard NA  0  1  1  1  1  1  1  1  NA
  4       2  baboon NA  0  0  1  0  1  0  1  0  NA
  5       2 caracal NA  0  0  0  0  0  0  0  0  NA
  6       2 leopard NA  0  0  0  1  1  0  1  0  NA
  7       3  baboon  0  0  1  0  1  1  0  0  0   0
  8       3 caracal  0  0  1  0  1  1  1  0  1   1
  9       3 leopard  1  1  0  0  0  1  0  0  1   0
  10      4  baboon  1  0  1  1  0  0  0  0  1   0
  11      4 caracal  1  0  0  1  1  0  0  1  1   0
  12      4 leopard  0  1  0  0  1  1  0  0  0   0
  13      5  baboon NA NA  1  0  1  1  1  0 NA  NA
  14      5 caracal NA NA  0  0  0  1  0  0 NA  NA
  15      5 leopard NA NA  0  0  0  0  0  0 NA  NA
  </code></pre>