title: Replacing NA with a value depending on the condition
body: <pre><code>&gt; str(store)
  'data.frame':   1115 obs. of  10 variables:
   $ Store                    : int  1 2 3 4 5 6 7 8 9 10 ...
   $ StoreType                : Factor w/ 4 levels "a","b","c","d": 3 1 1 3 1 1 1 1 1 1 ...
   $ Assortment               : Factor w/ 3 levels "a","b","c": 1 1 1 3 1 1 3 1 3 1 ...
   $ CompetitionDistance      : int  1270 570 14130 620 29910 310 24000 7520 2030 3160 ...
   $ CompetitionOpenSinceMonth: int  9 11 12 9 4 12 4 10 8 9 ...
   $ CompetitionOpenSinceYear : int  2008 2007 2006 2009 2015 2013 2013 2014 2000 2009 ...
   $ Promo2                   : int  0 1 1 0 0 0 0 0 0 0 ...
   $ Promo2SinceWeek          : int  NA 13 14 NA NA NA NA NA NA NA ...
   $ Promo2SinceYear          : int  NA 2010 2011 NA NA NA NA NA NA NA ...
   $ PromoInterval            : Factor w/ 4 levels "","Feb,May,Aug,Nov",..: 1 3 3 1 1 1 1 1 1 1 ...
  </code></pre>
  
  <p>I'm trying to replace NA's depending on Promo2 value.If Promo2==0, NA values in that row need to be zero, else if Promo2==1 missing values should be replaced by column mean.</p>
  
  <p>Don't understand why my code doesn't edit store data.</p>
  
  <pre><code>for (i in 1:nrow(store)){
    if(is.na(store[i,])== TRUE &amp; store$Promo2[i] ==0){
      store[i,] &lt;- ifelse(is.na(store[i,]),0,store[i,])
    }
    else if (is.na(store[i,])== TRUE &amp; store$Promo2[i] ==1){
      for(j in 1:ncol(store)){
        store[is.na(store[i,j]), j] &lt;- mean(store[,j], na.rm = TRUE)
      }
    }
  }
  </code></pre>