2

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")
    ```
  • 1
    Try converting to `character`. class i.e. `df %>% mutate_at(c(1:7, 15:24), as.character) %>%` and then your code – akrun May 16 '20 at 22:05
  • 2
    `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 `?recode` – akrun May 16 '20 at 22:07
  • @akrun thank you for replying. As I mentioned, even if I didn't add missing command, the error was same. I tried running with ```.missing = NULL```, the error message is still the same. _unused argument_. – Ritika Khurana May 17 '20 at 00:48
  • Could you please elaborate what do you mean by converting to ```as.character``` ? I did not quite understand. – Ritika Khurana May 17 '20 at 00:49
  • Hello @RitikaKhurana, could you share some sample data? It would be easy for us to help you if we can test on data. – Alexis May 17 '20 at 01:39
  • ``` Country HHCode E301 E302 E303 B201 B202 B203 E307 1 122042 3 3 3 3 2 3 3 1 123032 3 2 2 4 2 3 3 1 123027 3 4 3 4 2 3 3 1 123028 3 4 3 4 2 3 3 1 N123001 3 3 3 3 2 3 3 1 123041 3 3 3 3 2 3 3 1 123009 3 3 3 3 2 3 3 1 123011 3 3 3 2 2 2 3 1 123010 3 3 3 3 2 2 3 1 123038 2 3 3 3 2 3 3 1 N123002 2 3 3 3 3 3 ``` @alexismenanieves Sorry for format of the data. I looked up for how to add data. I could only find that it's uploaded in ASCII format, but couldn't figure the format button in comments. – Ritika Khurana May 17 '20 at 03:02
  • 1
    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 http://stackoverflow.com/questions/5963269 which shares how to include a reproducible example. – Ronak Shah May 17 '20 at 04:02
  • 1
    @RonakShah that was a useful advice on adding sample data. It worked fine! – Ritika Khurana May 17 '20 at 04:24
  • 1
    @RitikaKhurana i posted my comment as a solution. the issue is the `na.rm` – akrun May 17 '20 at 18:51

2 Answers2

1

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')
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Unfortunately, I get the same error. Error in (function (var, recodes, as.factor, as.numeric = TRUE, levels) : unused arguments (`2` = "1", `3` = "1", `1` = "0", `4` = "0", .default = NA) – Ritika Khurana May 17 '20 at 04:11
  • 1
    @RitikaKhurana I don't get that error on the data that you have shared. Can you restart R, load only `dplyr` and try again? – Ronak Shah May 17 '20 at 04:50
  • You were right, I just need to load only ```dplyr``` library. Now, the error is fixed but when I view the variables, I find no change occurred to them. They are still the same. Codes did not change. Do I need to create new variables or a new data frame for change to manifest? – Ritika Khurana May 17 '20 at 05:35
  • 1
    Yes, you need to assign the values back. `df1 <- df %>% mutate_at(1:7, recode, '2'='1', '3'='1', '1'='0', '4'='0') %>% mutate_at(15:24, recode, '2'='0')` – Ronak Shah May 17 '20 at 05:40
  • Thank you very much! my problem is fixed. – Ritika Khurana May 17 '20 at 05:43
1

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')
akrun
  • 874,273
  • 37
  • 540
  • 662