A nice clean one line in data.table
library(data.table)
setDT(DF)
TWO OPTIONS:
results as a list
DF[ , .(id = list(id)), by = brand]
brand id
1: RadioShack 2308
2: Rag & Bone 4466
3: Ragu 1830,4518
4: Ralph Lauren 1638,2719,2720,2721,2722
>
results as a string
DF[ , .(id = paste(id, collapse=",")), by = brand]
brand id
1: RadioShack 2308
2: Rag & Bone 4466
3: Ragu 1830,4518
4: Ralph Lauren 1638,2719,2720,2721,2722
Note
Even though the two results appear the same (that is when you print them, they look identical), they are in fact very different and allow for different functionality.
Namely, using the list option (the first one) allows you to then perform functions on the orignal id
s.
The latter will allow you to display the information more easily (including exporting to CSV
or excel
), but to operate on the id
's will require splicing them back.