r - Allocating id/factor/categories based on a group of values for each factor -
i have large data table following:
id var1 var2 1 1 2 2 d 3 6 d 4 4 b 5 6 d 6 8
i need assign category in var2
based on values in var1
. categories not follow order respect var1
values included in each category. instance:
lista <- c(1,5,7) listb <- c(4,9) listd <- c(2,6)
i have tried 2 approaches unsuccessfully. using which
function:
which: dt[which(var1 %in% lista), var2 := "a"]
, on listb
, listd
.
it didn't work function approach
(which may slow large data table have many elseif
clauses). wrote:
matchfun <- function(value){ if (var1 %in% lista){ value <- as.character(a)} else { return(value)}}
any idea or comment on how allocate factor/categories group of values welcome.
i'd suggest merge here. let dt
original data table.
dt <- data.table(id=1:6,var1=c(1,2,6,4,6,8))
first, need store mapping in table:
matchdt <- rbindlist(list( data.table(var1=lista,var2="a"), data.table(var1=listb,var2="b"), data.table(var1=listd,var2="d") ))
then can merge, optionally setting id
key afterward restore original sorting.
setkey(dt,var1) dt[matchdt,var2:=var2,nomatch=false] setkey(dt,id)
the result is
id var1 var2 1: 1 1 2: 2 2 d 3: 3 6 d 4: 4 4 b 5: 5 6 d 6: 6 8 na
the last value na
because lista
object doesn't contain 8
(but should).
Comments
Post a Comment