-1

I have a dataframe like this say n

id subject 
-------------
1  discount less

2  product good

3  product good

4  wonderful service

5  discount less

and another dataframe say p like this

Subject   Rate
----------------
product good  20

wonderful service 30

discount less  10

i want the output as :

id   subject  rate
--------------------
1,5   discount less

2,3   product good

4    wonderful service

if I match like p$id <- n$id[match(p$subject,n$subject)] then only the first element matched will be shown...but i want all the ids....

Can anyone guide me on this

Sotos
  • 51,121
  • 6
  • 32
  • 66
Prajna
  • 129
  • 1
  • 8
  • 1
    [How to make a great R reproducible example?](http://stackoverflow.com/questions/5963269) – Sotos Jan 10 '18 at 13:32
  • Hard to give you an exact answer without a reproducible example, but this is a start: `sapply(p$subject, function(x) paste0(n[n$subject %in% x, "id"], collapse = ","))` – Mike H. Jan 10 '18 at 13:41

1 Answers1

1

how about something like this:

n$subject<-as.character(n$subject)
id=sapply(unique(n$subject),function(x) paste(as.character(n[n$subject==x,]$id), collapse=", "))
subject=unique(n$subject)
df1=data.frame(id=id,subject=subject)
df2=merge(df1,p,by="subject")
df2=df2[c("id", "subject", "Rate")]
Antonios
  • 1,919
  • 1
  • 11
  • 18