Search code examples
rrecode

R recoding using .default got length error


My database looks like below. enter image description here

I try the following code, in order to recode "EDUC" column.

gun.control$EDUC <- recode(gun.control$EDUC,
                       'IAP' = NULL,
                       'DK' = NULL,
                       .default = gun.control$EDUC,
                       .missing = NULL)

However, there shows an error saying "Error: '.default' must be length 24 or one, not 62466.". I've try length(gun.control$EDUC) and it is 62366 long. The result of unique(gun.control$EDUC) shows Levels: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 IAP DK NA

I dont know in which part I did it wrong and resulting in this problem. Anyone can possible see any solution here? Really appreciate a lot.


Solution

  • You probably have NA values in the data hence recode returns an error. Also instead of NULL you should replace the data with NA. :

    gun.control$EDUC[gun.control$EDUC %in% c('IAP', 'DK')] <- NA