-4

The data is like this

1.  CM-00063262-15  EARRINGS
2.  CM-00063262-15  EARRINGS
3.  CM-00063262-15  NECKLACE
4.  CM-00063262-15  WALLET-WOMEN'S
5.  CM-00063263-15  SLACKS
6.  CM-00063264-15  BATH TUB
7.  CM-00063264-15  GIFT SET

I want output like this

1.  CM-00063262-15  EARRINGS,EARRINGS,NECKLACE,WALLET-WOMEN'S
2.  CM-00063263-15  SLACKS
3.  CM-00063264-15  BATH TUB,GIFT SET

Thank you in advance

user2100721
  • 3,557
  • 2
  • 20
  • 29
  • Please read [(1)](http://stackoverflow.com/help/how-to-ask) how do I ask a good question, [(2)](http://stackoverflow.com/help/mcve) How to create a MCVE as well as [(3)](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#answer-5963610) how to provide a minimal reproducible example in R. Then we can help you. – Christoph Jul 01 '16 at 11:06

2 Answers2

0

We need to extract the bill number and use it as grouping variable,

library(data.table)
setDT(df1)[,  toString(unique(sub("\\S+", "", Col))), 
                        by = .(grp = sub("\\s+.*", "", Col))]
#             grp                                    V1
#1: CM-00063262-15  EARRINGS,  NECKLACE,  WALLET-WOMEN'S
#2: CM-00063263-15                                SLACKS
#3: CM-00063264-15                   BATH TUB,  GIFT SET

If the OP's dataset have two columns instead of one, it is much easier

setDT(df1)[, toString(unique(Col2)), by = Col1]

data

df1 <- structure(list(Col = c("CM-00063262-15 EARRINGS", 
 "CM-00063262-15 EARRINGS", 
"CM-00063262-15 NECKLACE", "CM-00063262-15 WALLET-WOMEN'S", "CM-00063263-15 SLACKS", 
"CM-00063264-15 BATH TUB", "CM-00063264-15 GIFT SET")),
 .Names = "Col", class = "data.frame", row.names = c(NA, -7L))
akrun
  • 874,273
  • 37
  • 540
  • 662
0

Use this

aggregate(data=df,V2~V1,FUN=paste)
user2100721
  • 3,557
  • 2
  • 20
  • 29