dput(new)
structure(list(ID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22), A1 = c(1, 1, 1, 1, 0,
0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
Here is a one liner via base R using stack and aggregate,
aggregate(ind ~ ID,
subset(cbind(ID = new$ID, stack(replace(new, new == 0, '')[-1])), values == 1),
toString)
which gives,
ID ind 1 1 A1, A2 2 2 A1, A2 3 3 A1 4 4 A1 5 6 A2, A8 6 7 A6, A8 7 8 A1, A8 8 9 A6, A8 9 10 A8 10 11 A1, A8 11 12 A6 12 13 A5, A8 13 15 A8 14 16 A8 15 17 A8 16 18 A8 17 19 A8 18 20 A8 19 21 A7