lundi 6 septembre 2021

In R ,how to collapse character into new group

I R, I want to add new character group 'region_new ', it seems 'if else' not work. Any one can help ? thank

test_data <- data.frame(region=LETTERS)

test_data$region_new <- ''
if (test_data$region in c('a','b','c')){
   test_data$region_new <- 'Level_A'
} else if (test_data$region in c('d','x','z')) {
   test_data$region_new <- 'Level_B'
} else {
   test_data$region_new <- 'Level_Other'
}

Aucun commentaire:

Enregistrer un commentaire