0

Imagine a data.table like this

library(data.table)
DT = data.table(values=c('call', NA, 'letter', 'call', 'e-mail', 'phone'))
print(DT)

   values
1:   call
2:   <NA>
3: letter
4:   call
5: e-mail
6:  phone

I wish to recode the values by the following mapping

mappings = list(
  'by_phone' = c('call', 'phone'),
  'by_web' = c('e-mail', 'web-meeting')
)

I.e. I want to transform call into by_phone etc. NA should be put to missing and unknown (by the mapping provided) put to other. For this particular data table I could simply solve my problem by the following

recode_group <- function(values, mappings){
  ifelse(values %in% unlist(mappings[1]), names(mappings)[1], 
         ifelse(values %in% unlist(mappings[2]), names(mappings)[2], 
                ifelse(is.na(values), 'missing', 'other')
         )
    )
}
DT[, recoded_group:=recode_group(values, mappings)]
print(DT)

   values recoded_group
1:   call      by_phone
2:   <NA>       missing
3: letter         other
4:   call      by_phone
5: e-mail        by_web
6:  phone      by_phone

But I am looking for an efficient and generic recode_group functionality. Any suggestions?

talat
  • 68,970
  • 21
  • 126
  • 157
mr.bjerre
  • 2,384
  • 2
  • 24
  • 37

1 Answers1

1

Here's an option with an update-join approach:

DT[stack(mappings), on = "values", recoded_group := ind]
DT[is.na(values), recoded_group := "missing"]

DT
#   values recoded_group
#1:   call      by_phone
#2:     NA       missing
#3: letter            NA
#4:   call      by_phone
#5: e-mail        by_web
#6:  phone      by_phone
talat
  • 68,970
  • 21
  • 126
  • 157