2
votes

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")
    ```
2
Try converting to character. class i.e. df %>% mutate_at(c(1:7, 15:24), as.character) %>% and then your codeakrun
dplyr::recode doesn't have na.rm argument. It is .missing = NULL. (by default). When in doubt about a functions arguments, you can check with help("recode") or ?recodeakrun
Don't use attach, further you can add your data by clicking on edit at the bottom of your post and including dput(head(df)). This is a good reference post stackoverflow.com/questions/5963269 which shares how to include a reproducible example.Ronak Shah
@RonakShah that was a useful advice on adding sample data. It worked fine!Ritika Khurana
@RitikaKhurana i posted my comment as a solution. the issue is the na.rmakrun

2 Answers

1
votes

Try using :

library(dplyr)

df %>%
  mutate_at(1:7, recode, '2'='1', '3'='1', '1'='0', '4'='0') %>%
  mutate_at(15:24, recode, '2'='0')
1
votes

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')