title: Conditionally replace certain values in my dataframe with other values in the dataframe in R
body: <p>I have seen this <a href="https://stackoverflow.com/questions/34096162/dplyr-mutate-replace-several-columns-on-a-subset-of-rows">dplyr mutate/replace several columns on a subset of rows</a> and this <a href="https://stackoverflow.com/questions/8214303/conditional-replacement-of-values-in-a-data-frame">Conditional replacement of values in a data.frame</a></p>
  <p>I want to conditionally replace certain values in my dataframe with other values in my dataframe, based on the nested structure of my data.</p>
  <p>I have a dataframe which, in some cases, has multiple rows representing a single study. In these cases, I have data representing overall ratings of the study (where <code>effect</code> = &quot;All.outcomes.&quot;) followed by rows representing ratings of specific effects within the study (<code>effect</code> = &quot;Study.1..Effect 1&quot;, for example). The rows representing specific effects nested within a study always have missing data on a number of variables. In these cases, I would like to replace the NA values in the specific effect rows with data from the &quot;All.outcomes&quot; row for each StudyID.</p>
  <p>Missing data is always on given variables: <code>q1</code>, <code>q2B</code>, <code>q2C</code>, <code>q2A</code>, and <code>q4</code>. In the cases where there is missing data on these variables, I would like to fill in the data from the line where <code>effects</code> = &quot;All.outcomes.&quot;</p>
  <p>Here's what it looks like now:</p>
  <pre><code>     studyID         effect  q1  q11 q2B  q2C q2a   q4   q9
  a    s100      All.outcomes low   NA low high low  low   NA
  b    s100 Study.1..Effect.1  NA high  NA   NA  NA   NA  low
  c    s100 Study.1..Effect.2  NA high  NA   NA  NA   NA  low
  d    s101      All.outcomes low  low low high low  low  low
  e    s102      All.outcomes low  low low high low high  low
  f    s104      All.outcomes low   NA low high low  low   NA
  g    s104 Study.1..Effect.1  NA  low  NA   NA  NA   NA  low
  h    s104 Study.2..Effect.1  NA high  NA   NA  NA   NA high
  i    s104 Study.3..Effect.1  NA  low  NA   NA  NA   NA  low
  </code></pre>
  <p>Here's how I want the dataframe to look:</p>
  <pre><code>     studyID         effect  q1  q11 q2B  q2C q2a   q4   q9
  a    s100      All.outcomes low   NA low high low  low   NA
  b    s100 Study.1..Effect.1 low high low high low  low  low
  c    s100 Study.1..Effect.2 low high low high low  low  low
  f    s104      All.outcomes low   NA low high low  low   NA
  g    s104 Study.1..Effect.1 low  low low high low  low  low
  h    s104 Study.2..Effect.1 low high low high low  low high
  i    s104 Study.3..Effect.1 low  low low high low  low  low
  </code></pre>
  <p>You can see that, for example, for s100 <code>effect</code> = &quot;Study.1..Effect.1&quot; and &quot;Study.1..Effect.2&quot;, I filled in <code>q1</code>, <code>q2B</code>, <code>q2C</code>, <code>q2A</code>, and <code>q4</code> with values from <code>effect</code> = `All.outcomes.&quot;</p>
  <p>Note that here are studyIDs in the database where this pattern does not occur (i.e., they only have <code>effect</code> = &quot;All.outcomes&quot;) and that in cases where a given study does have multiple effects nested within it, the number and names of these nested effects vary (as can be seen comparing <code>studyID</code> = s100 and s104 in the sample data).</p>
  <p>Is there an efficient solution to this? Thank you in advance for your help.</p>
  <pre><code>#sample data
  a &lt;- c(&quot;s100&quot;, &quot;All.outcomes&quot;, &quot;low&quot;, &quot;NA&quot;, &quot;low&quot;, &quot;high&quot;, &quot;low&quot;, &quot;low&quot;, &quot;NA&quot;)
  b &lt;- c(&quot;s100&quot;,  &quot;Study.1..Effect.1&quot;,    &quot;NA&quot;,   &quot;high&quot;, &quot;NA&quot;,   &quot;NA&quot;,   &quot;NA&quot;,   &quot;NA&quot;,   &quot;low&quot;)
  c &lt;- c(&quot;s100&quot;,  &quot;Study.1..Effect.2&quot;,    &quot;NA&quot;,   &quot;high&quot;, &quot;NA&quot;,   &quot;NA&quot;,   &quot;NA&quot;,   &quot;NA&quot;,   &quot;low&quot;)
  d &lt;- c(&quot;s101&quot;,  &quot;All.outcomes&quot;, &quot;low&quot;,  &quot;low&quot;,  &quot;low&quot;,  &quot;high&quot;, &quot;low&quot;,  &quot;low&quot;,  &quot;low&quot;)
  e &lt;- c(&quot;s102&quot;,  &quot;All.outcomes&quot;, &quot;low&quot;,  &quot;low&quot;,  &quot;low&quot;,  &quot;high&quot;, &quot;low&quot;,  &quot;high&quot;, &quot;low&quot;)
  f &lt;- c(&quot;s104&quot;,  &quot;All.outcomes&quot;, &quot;low&quot;,  &quot;NA&quot;,   &quot;low&quot;,  &quot;high&quot;, &quot;low&quot;,  &quot;low&quot;,  &quot;NA&quot;)
  g &lt;- c(&quot;s104&quot;,  &quot;Study.1..Effect.1&quot;,    &quot;NA&quot;,   &quot;low&quot;,  &quot;NA&quot;,   &quot;NA&quot;,   &quot;NA&quot;,   &quot;NA&quot;,   &quot;low&quot;)
  h &lt;- c(&quot;s104&quot;,  &quot;Study.2..Effect.1&quot;,    &quot;NA&quot;,   &quot;high&quot;, &quot;NA&quot;,   &quot;NA&quot;,   &quot;NA&quot;,   &quot;NA&quot;,   &quot;high&quot;)
  i &lt;- c(&quot;s104&quot;,  &quot;Study.3..Effect.1&quot;,    &quot;NA&quot;,   &quot;low&quot;,  &quot;NA&quot;,   &quot;NA&quot;,   &quot;NA&quot;,   &quot;NA&quot;,   &quot;low&quot;)
  
  df &lt;- as.data.frame(rbind(a,b,c,d,e,f,g,h,i))
  colnames(df)=c(&quot;studyID&quot;, &quot;effect&quot;, &quot;q1&quot;, &quot;q11&quot;, &quot;q2B&quot;, &quot;q2C&quot;, &quot;q2a&quot;, &quot;q4&quot;, &quot;q9&quot;)
  </code></pre>