You could do this in one line using plyr
, as it is a classic split-apply-combine
problem. You split using SNP
, apply paste
with collapse
and assemble the pieces back into a data frame.
plyr::ddply(x, .(SNP), colwise(paste), collapse = ",")
If you want to do data
reshaping in R at the flick of a wrist
, learn plyr
and reshape2
:). Another flick of the wrist solution using data.table
, really useful if you are dealing with massive amounts of data.
data.table::data.table(x)[,lapply(.SD, paste, collapse = ","),'SNP']