-1

I am working with a medium size dataset and I am interested in recoding several variables at once.

There are 15 variables coded as factors with three levels. YES=3, NO=2, N/A=1. I would like to recode all 15 variables as numeric. YES=1, NO=0, N/A=NA.

Prior to updating my software, this code worked.

my_data[, 9:23 := lapply(.SD, recode, "'YES'=1;'NO'=0;'N/A'=NA", as.factor.result= FALSE), .SDcols = 9:23] 

Now I am receiving an error "Error: Argument 2 must be named, not unnamed" Please let me know what I am doing wrong/missing here! Thanks in advance!

1 Answers1

0

The following works:

library(dplyr)
library(data.table)

set.seed(10)
sampler <- function() as.character(sample(c(1:3), 20, TRUE))
my_data <- data_frame(
    id = 1:20,
    a = sampler(),
    b = sampler(),
    c = sampler()
)

dt <- data.table(copy(my_data))

recoder <- function(x) {
    x <- as.integer(x) - 2
    x[x < 0] <- NA
    x
}

## data.table approach
cols <- colnames(dt)[-1]
dt[ ,(cols) := lapply(.SD, recoder), .SDcols = cols][]
dt

## dplyr approach
my_data  %>%
    mutate_at(vars(a:c), recoder)
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • Hey thanks for the reply. Unfortunately, the data.table way returns only NAs and the dplyr method has zero impact. Am I doing something wrong? These are character values that (when I have previously converted them to numeric variable by variable) have shown up as 3,2,1. Is there something I am doing wrong? – kemanli Oct 17 '17 at 15:08
  • I actually found a way to modify my original code: my_data[, 9:23 := lapply(.SD, recode, `YES`=1,`NO`=0), .SDcols = 9:23] – kemanli Oct 18 '17 at 19:28