Making a character string with column names with zero values

后端 未结 3 1468
春和景丽
春和景丽 2021-01-25 01:58

The 4th column is my desired column. Video,Webinar,Meeting,Conference are the 4 type of activities that the different customers(names) can engage in. You can see,in a given row,

3条回答
  •  孤独总比滥情好
    2021-01-25 02:09

    Here you go:

    setcolorder(dt, c("Name", "Webinar", "Meeting", "Conference", "Video", "NextStep"))
    dt[, NextStepNew:=apply(dt, 1, function(x) paste0(names(x)[x==0], collapse=","))][]
       Name Webinar Meeting Conference Video                   NextStep                NextStepNew
    1: John       0       0          0     1 Webinar,Meeting,Conference Webinar,Meeting,Conference
    2: John       1       0          0     1         Meeting,Conference         Meeting,Conference
    3: John       1       1          0     1                 Conference                 Conference
    4:  Tom       0       1          0     0   Webinar,Conference,Video   Webinar,Conference,Video
    5:  Tom       0       1          1     0              Webinar,Video              Webinar,Video
    6: Kyle       0       0          1     0      Webinar,Meeting,Video      Webinar,Meeting,Video
    

提交回复
热议问题