title: How to hand over a list of vectors to a function?
body: <p>I have a dataframe and some of the vectors have NAs. I'd like to write a function that recodes NA to 0. However, I don't wan't to recode all vectors. I'd rather prefer specifying a list of vectors to which I can refer to when calling the function. The vectors should be recoded within the dataframe.</p>
  <p>Here is my dataframe:</p>
  <pre><code>structure(list(ahvn13_chiff = structure(1:6, .Label = c(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;, &quot;f&quot;, &quot;g&quot;, &quot;h&quot;,&quot;i&quot;
  ), class = c(&quot;labelled&quot;, &quot;factor&quot;), label = c(ahvn13_chiff = &quot;Sozialversicherungsnummer anonymisiert&quot;)), 
      sex = structure(c(1L, 1L, 1L, 1L, 2L, 2L), label = c(sex = &quot;Geschlecht&quot;), class = c(&quot;labelled&quot;, 
      &quot;integer&quot;)), yearmonthday_dateofbirth = structure(c(11086070400, 
      13038710400, 11838355200, 13972348800, 13959993600, 10880870400
      ), label = c(yearMonthDay_dateOfBirth = &quot;Geburtsdatum&quot;), class = c(&quot;labelled&quot;, 
      &quot;numeric&quot;)), z100 = c(0L, 1331L, NA, NA, 0L, NA), z102 = c(0L, 
      222004L, 0L, 25004L, 0L, NA), z104 = c(0L, NA, 0L, NA, 0L, 
      NA), z106 = c(0L, 3604L, 0L, 0L, 0L, NA)), codepage = 65001L, row.names = c(NA, 
  6L), class = &quot;data.frame&quot;) 
  </code></pre>
  <p>I managed to write a code that recodes the NAs of some of the vectors:</p>
  <pre><code>data[c(&quot;z100&quot;,&quot;z102&quot;)][is.na(data[c(&quot;z100&quot;,&quot;z102&quot;)])] &lt;- 0
  </code></pre>
  <p>I then tried to convert this code to a function and ended up with these two options:</p>
  <pre><code>na.zero &lt;- function(x) {
   x[is.na(x)] &lt;- 0
  }
  
  na.zero(data[c(&quot;z100&quot;,&quot;z102&quot;)])
  </code></pre>
  <pre><code>na.zero &lt;- function(df,var) {
   df[var][is.na(df[var])] &lt;- 0
  }
  
  na.zero(data, c(&quot;z100&quot;,&quot;z102&quot;))
  </code></pre>
  <p>However, none of them works.
  What is wrong and how else can I hand over a list of vectors to a function.</p>
  <p>Thank you in advance for your Inputs.</p>