简体   繁体   中英

R recoding using .default got length error

My database looks like below. 在此处输入图像描述

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.

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

We can use replace

library(dplyr)
gun.control %>%
        mutate(EDUC = replace(EDUC, EDUC %in% c("IAP", "DK"), NA_character_))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM