title: How to Apply functions to specific set of columns in data frame in R to replace NAs
body: <p>I have a data set in which I want to replace NAs in different columns differently. Following is the dummy data set and code to replicate it .</p>
  
  <pre><code>test &lt;- data.frame(ID = c(1:5),
                 FirstName = c(NA,"Sid",NA,"Harsh","CJ"),
                 LastName = c("Snow",NA,"Lapata","Khan",NA),
                 BillNum = c(6:10),
                 Phone = c(1213,3123,3123,NA,NA),
                 Married = c("Yes","Yes",NA,"NO","Yes"),
                 ZIP = c(1111,2222,333,444,555),
                 Gender = c("M",NA,"F",NA,"M"),
                 Address = c("A","B",NA,"C","D"))
  &gt; test
    ID FirstName LastName BillNum Phone Married  ZIP Gender Address
  1  1      &lt;NA&gt;     Snow       6  1213     Yes 1111      M       A
  2  2       Sid     &lt;NA&gt;       7  3123     Yes 2222   &lt;NA&gt;       B
  3  3      &lt;NA&gt;   Lapata       8  3123    &lt;NA&gt;  333      F    &lt;NA&gt;
  4  4     Harsh     Khan       9    NA      NO  444   &lt;NA&gt;       C
  5  5        CJ     &lt;NA&gt;      10    NA     Yes  555      M       D
  </code></pre>
  
  <p>In some columns I want to indicate if a value was supplied by customer or not without retaining the supplied value as following.</p>
  
  <pre><code>Availability_Indicator &lt;- function(x){
    x &lt;- ifelse(is.na(x),"NotAvialable","Available")
    return(x)
  }
  test$FirstName &lt;- Availability_Indicator(test$FirstName)
  test$LastName &lt;- Availability_Indicator(test$LastName)
  test$Phone &lt;- Availability_Indicator(test$Phone)
  test$Address &lt;- Availability_Indicator(test$Address)
  </code></pre>
  
  <p>I get the following Data </p>
  
  <pre><code>&gt; test
  ID    FirstName     LastName BillNum        Phone Married  ZIP Gender
   1 NotAvialable    Available       6    Available     Yes 1111      M
   2    Available NotAvialable       7    Available     Yes 2222   &lt;NA&gt; 
   3 NotAvialable    Available       8    Available    &lt;NA&gt;  333      F
   4    Available    Available       9 NotAvialable      NO  444   &lt;NA&gt;
   5    Available NotAvialable      10 NotAvialable     Yes  555      M
  
  Address
  Available
  Available
  NotAvialable
  Available
  Available
  </code></pre>
  
  <p>In married and gender variable I dont want to lose the value of column and just replace the NAs as following.</p>
  
  <pre><code>NotAvailable_Indicator &lt;- function(x){
    x[is.na(x)]&lt;-"NotAvailable"
    return(x)
  }
  test$Married &lt;- NotAvailable_Indicator(test$Married)
  test$Gender &lt;- NotAvailable_Indicator(test$Gender)
  </code></pre>
  
  <p>I get the following data set.</p>
  
  <pre><code>ID    FirstName     LastName BillNum        Phone      Married  ZIP       Gender      Address
   1 NotAvialable    Available       6    Available          Yes 1111            M    Available
   2    Available NotAvialable       7    Available          Yes 2222 NotAvailable    Available
   3 NotAvialable    Available       8    Available NotAvailable  333            F NotAvialable
   4    Available    Available       9 NotAvialable           NO  444 NotAvailable    Available
   5    Available NotAvialable      10 NotAvialable          Yes  555            M    Available
  </code></pre>
  
  <p>My problem is that I dont want to repeat the function calls for each column separately as I have about 200 columns. I was not able to use apply functions as I had to subset data then apply the functions using lapply and then cbind again to original data which changed the order of columns. Is there any method where I can supply names of column and the function and I get modified columns along with other columns(which were not changed) in return as a data set or the columns are modified inplace without returning anything(like DataFrame.fillna in python which has argument inplace=logical) </p>