Вы можете немного изменить свою таблицу и затем снова присоединиться.
library(data.table)
dt <- data.table(Race_Asian = c("Y","N","N"),
Race_Black=c('N',"Y","N"),
Race_Hispanic = c("N","Y","Y"))
dt[,id:=.I]
# example table
dt
#> Race_Asian Race_Black Race_Hispanic id
#> 1: Y N N 1
#> 2: N Y Y 2
#> 3: N N Y 3
#reshape solution
ldt <- melt(dt, id.vars = "id",variable.name = "Race_col",value.name = "Response")
ldt
#> id Race_col Response
#> 1: 1 Race_Asian Y
#> 2: 2 Race_Asian N
#> 3: 3 Race_Asian N
#> 4: 1 Race_Black N
#> 5: 2 Race_Black Y
#> 6: 3 Race_Black N
#> 7: 1 Race_Hispanic N
#> 8: 2 Race_Hispanic Y
#> 9: 3 Race_Hispanic Y
ldt[,Race1:=fifelse(Response=="Y",sub("Race_","",Race_col),NA_character_)]
ldt[,Race:=paste0(Race1[!is.na(Race1)],collapse = "|"),by=.(id)]
ldt
#> id Race_col Response Race1 Race
#> 1: 1 Race_Asian Y Asian Asian
#> 2: 2 Race_Asian N <NA> Black|Hispanic
#> 3: 3 Race_Asian N <NA> Hispanic
#> 4: 1 Race_Black N <NA> Asian
#> 5: 2 Race_Black Y Black Black|Hispanic
#> 6: 3 Race_Black N <NA> Hispanic
#> 7: 1 Race_Hispanic N <NA> Asian
#> 8: 2 Race_Hispanic Y Hispanic Black|Hispanic
#> 9: 3 Race_Hispanic Y Hispanic Hispanic
# you can join result back
dt[ldt[,.(Race),by=.(id)],Race:=i.Race,on=.(id)]
dt
#> Race_Asian Race_Black Race_Hispanic id Race
#> 1: Y N N 1 Asian
#> 2: N Y Y 2 Black|Hispanic
#> 3: N N Y 3 Hispanic
Создано в 2020-04-22 с помощью пакета Представить (v0.3.0)