title: recoding variables in a loop in R
body: <p>I want to recode several variables together. All these variables will undergo same recoding change. 
  For this, I followed the thread below. The thread below describes two ways of doing it. 
  1). Using column number 
  2). using variable names</p>
  
  <p>I tried both but I get an error message.</p>
  
  <p>Error message for 1) and 2). 
  Error in (function (var, recodes, as.factor, as.numeric = TRUE, levels)  : 
    unused arguments (<code>2</code> = "1", <code>3</code> = "1", <code>1</code> = "0", <code>4</code> = "0", na.rm = TRUE)</p>
  
  <p><a href="https://stackoverflow.com/questions/51450153/recode-variable-in-loop-r">recode variable in loop R</a></p>
  
  <pre><code>#Uploading libraries
  library(dplyr)
  library(magrittr)
  library(plyr)
  library(readxl)
  library(tidyverse)
  
  #Importing file
  mydata &lt;- read_excel("CCorr_Data.xlsx")
  df &lt;- data.frame(mydata)
  attach(df)
  
  #replacing codes for variables
  df %&gt;%
    mutate_at(c(1:7), recode, '2'='1', '3'='1', '1'='0', '4'='0', na.rm = TRUE) %&gt;%
    mutate_at(c(15:24), recode, '2'='0', na.rm = TRUE)
  
  
  df %&gt;% 
    mutate_at(vars(E301, E302, E303), recode,'2'='1', '3'='1', '1'='0', '4'='0', na.rm = TRUE) %&gt;%
    mutate_at(vars(B201, B202, B203), recode, '2'='0', na.rm = TRUE)
  
  </code></pre>
  
  <p>Can someone tell me where am I going wrong?</p>
  
  <p>In my dataset there are missing values that's why I have included <code>na.rm = T</code>. I even tried without including the missing value command, the error message was the same even then.</p>
  
  <p>Please see below for sample data.</p>
  
  <pre><code>structure(list(Country = c(1, 1, 1, 1, 1, 1), HHID = c("12ae5148e245079f-122042", 
  "12ae5148e245079f-123032", "12ae5148e245079f-123027", "12ae5148e245079f-123028", 
  "12ae5148e245079f-N123001", "12ae5148e245079f-123041"), HHCode = c("122042", 
  "123032", "123027", "123028", "N123001", "123041"), A103 = c(2, 
  2, 2, 2, 2, 2), A104 = c("22", "23", "23", "23", "23", "23"), 
      Community = c("Mehmada", "Dhobgama", "Dhobgama", "Dhobgama", 
      "Dhobgama", "Dhobgama"), E301 = c(3, 3, 3, 3, 3, 3), E302 = c(3, 
      2, 4, 4, 3, 3), E303 = c(3, 2, 3, 3, 3, 3), E304 = c(3, 4, 
      4, 4, 3, 3), E305 = c(3, 2, 3, 3, 3, 3), E306 = c(3, 3, 3, 
      3, 3, 3), E307 = c(3, 3, 3, 3, 3, 3), E308 = c(3, 1, 3, 3, 
      3, 3), B201.1 = c(NA, 1, 1, 1, 1, 1), B202.1 = c(NA, 1, 1, 
      1, 1, 1), B203.1 = c(NA, 1, 1, 2, 2, 1), B204.1 = c(NA, 2, 
      1, 2, 1, 1), B205.1 = c(NA, 2, 1, 2, 2, 2), B206.1 = c(NA, 
      1, 1, 1, 2, 1), B207.1 = c(NA, 2, 1, 2, 2, 1), B208.1 = c(NA, 
      2, 2, 2, 2, 2), B209.1 = c(NA, 2, 1, 1, 1, 1), B210.1 = c(NA, 
      1, 1, 1, 1, 1)), row.names = c(NA, 6L), class = "data.frame")
      ```
  </code></pre>