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, all the column names with zero value are in the final column(NextStep) and the value there(character string separated by commas) excludes the column name with non-zero value. The character strings(column names) in the final column usually appear in the column order with two exceptions. Webinar always appears first if it has a zero value and video always appears last if it has a zero value.
library(data.table)
dt <- fread('
Name Video Webinar Meeting Conference NextStep
John 1 0 0 0 Webinar,Meeting,Conference
John 1 1 0 0 Meeting,Conference
John 1 1 1 0 Conference
Tom 0 0 1 0 Webinar,Conference,Video
Tom 0 0 1 1 Webinar,Video
Kyle 0 0 0 1 Webinar,Meeting,Video
')
My question is how to create the next step column. Thanks a lot for your help!
A possible solution:
which gives:
When you want to order the names as you specified in the comments, you can do:
which gives:
Instead of using
paste0
(withcollapse = ','
) you can also usetoString
.Used data:
In case you are looking for a way to do this without simply re-ordering the columns in the order you want (in fact I see no reason why not to do so, but anyway..) you could try the following approach. It
melt
s and updates by reference in a join:If you want to paste all column names as in the data for those cases where there's no activity, you can add the following line to your code:
Here you go: