[R] Ids with matching number combinations?

Jim Lemon drj|m|emon @end|ng |rom gm@||@com
Sat Oct 8 12:58:03 CEST 2022

Hi Marine,
I'm not sure that I have the right idea, but this worked for me and may help.

# I had to fix this line
df<-data.frame(id=rep(1:10,each=10),item1=sample(1:20, 100, replace=TRUE))
df2<-data.frame(a=c(8, 8,10,9, 5, 1,2,1),b=c(16,18,11, 19,18, 11,17,12))
# function that checks that all entries in vector a exist in vector x
match_all_a<-function(a,x,index) {
 for(i in 1:length(a)) inx <- inx && a[i] %in% x[-1]
# step through the data frame
for(i in df[,1]) result[i,]<-match_all_a(df2[i,],df[i,],i)
# display the result id, row in df, whether matched
# select the rows of result for which matched is TRUE


On Fri, Oct 7, 2022 at 11:57 PM Marine Andersson <marine.andersson using ki.se> wrote:
> Hi,
> If I have two datasets like this:
> df=data.frame("id"=rep(1:10,10, each=10), "item1"=sample(1:20, 100, replace=T)
> df2=data.frame("a"=c(8, 8,10,9, 5, 1,2,1), "b"=c(16,18,11, 19,18, 11,17,12))
> How do I find out which ids in the df dataset that has a match for both the numbers occuring in the same row in the df2 dataframe? In the output I would like to get the matching id and the rownumber from the df2.
> Output something like this
> Id                        Rownr
> 2                         1
> 5                         1
> 7                         4
> My actual problem is more complex with even more columns to be matched and the datasets are large, hence the solution needs to be efficient.
> Kind regards,
> N?r du skickar e-post till Karolinska Institutet (KI) inneb?r detta att KI kommer att behandla dina personuppgifter. H?r finns information om hur KI behandlar personuppgifter<https://ki.se/medarbetare/integritetsskyddspolicy>.
> Sending email to Karolinska Institutet (KI) will result in KI processing your personal data. You can read more about KI's processing of personal data here<https://ki.se/en/staff/data-protection-policy>.
>         [[alternative HTML version deleted]]
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list