using dplyr
, you can try:
dft %>%
group_by(Sample) %>%
summarise_each(funs( toString(unique(.))))
which gives:
# A tibble: 3 × 5
Sample group Gene1 Gene2 Gene3
1 A 1, 2 a, b NA NA
2 B 1 NA c NA
3 C 1, 2, 3 a, b, c NA d, e, NA
Edit:
@Axeman had the right idea with using na.omit(.)
to get rid of null values