Search code examples
rloopsdplyrrecode

recoding variables in a loop in R


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

I tried both but I get an error message.

Error message for 1) and 2). Error in (function (var, recodes, as.factor, as.numeric = TRUE, levels) : unused arguments (2 = "1", 3 = "1", 1 = "0", 4 = "0", na.rm = TRUE)

recode variable in loop R

#Uploading libraries
library(dplyr)
library(magrittr)
library(plyr)
library(readxl)
library(tidyverse)

#Importing file
mydata <- read_excel("CCorr_Data.xlsx")
df <- data.frame(mydata)
attach(df)

#replacing codes for variables
df %>%
  mutate_at(c(1:7), recode, '2'='1', '3'='1', '1'='0', '4'='0', na.rm = TRUE) %>%
  mutate_at(c(15:24), recode, '2'='0', na.rm = TRUE)


df %>% 
  mutate_at(vars(E301, E302, E303), recode,'2'='1', '3'='1', '1'='0', '4'='0', na.rm = TRUE) %>%
  mutate_at(vars(B201, B202, B203), recode, '2'='0', na.rm = TRUE)

Can someone tell me where am I going wrong?

In my dataset there are missing values that's why I have included na.rm = T. I even tried without including the missing value command, the error message was the same even then.

Please see below for sample data.

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")
    ```

Solution

  • The issue is with in the na.rm = TRUE, recode doesn't have that argument

    library(dplyr)   
    df %>% 
      mutate_at(vars(E301, E302, E303), recode,'2'='1', '3'='1', '1'='0', '4'='0') %>%
      mutate_at(vars(B201, B202, B203), recode, '2'='0')