如何使用R根据两个数据帧中一列的匹配提取所有值?

如何使用R根据两个数据帧中一列的匹配提取所有值?,r,dataframe,match,lookup,R,Dataframe,Match,Lookup,我有一个这样的数据框,比如说n id subject ------------- 1 discount less 2 product good 3 product good 4 wonderful service 5 discount less 另一个数据帧是这样的p Subject Rate ---------------- product good 20 wonderful service 30 discount less 10 i want the ou

我有一个这样的数据框,比如说n

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

2  product good

3  product good

4  wonderful service

5  discount less
另一个数据帧是这样的p

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

如果我匹配像
p$id这样的东西怎么样:

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")]

n$subject如果没有可复制的示例,很难给出准确的答案,但这是一个开始:
sapply(p$subject,函数(x)paste0(n[n$subject%in%x,“id”],collapse=“,”)