Hallo Marine Could you please make your example more reproducible by using set.seed (and maybe smaller)? If I understand correctly, you want to know if let say row 1 items from df2 (8,16) are both in item column of specific id? If I am correct in guessing, I cannot find another solution than split your df according to id x <- split(df, df$id)[[1]] and for each row of df2 test if within the specified id you can find both numbers. sum(is.element(df2[1,], x$item))==2 [1] FALSE So basically 2 cycles, one for df ids and the other for df2 rows. But maybe somebody will give you more ingenious answer. Cheers Petr> -----Original Message----- > From: R-help <r-help-bounces at r-project.org> On Behalf Of Marine Andersson > Sent: Friday, October 7, 2022 1:58 PM > To: r-help at r-project.org > Subject: [R] Ids with matching number combinations? > > 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 boththe> numbers occuring in the same row in the df2 dataframe? In the output Iwould> 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 KIkommer> att behandla dina personuppgifter. H?r finns information om hur KIbehandlar> personuppgifter<ki.se/medarbetare/integritetsskyddspolicy>. > > > Sending email to Karolinska Institutet (KI) will result in KI processingyour> personal data. You can read more about KI's processing of personal data > here<ki.se/en/staff/data-protection-policy>. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guideR-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Would an inner_join work? If not, please describe why so that we can improve our answer. This answer requires the dplyr package. statisticsglobe.com/r-dplyr-join-inner-left-right-full-semi-anti Regards, Tim -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of PIKAL Petr Sent: Friday, October 7, 2022 10:02 AM To: Marine Andersson <marine.andersson at ki.se>; r-help at r-project.org Subject: Re: [R] Ids with matching number combinations? [External Email] Hallo Marine Could you please make your example more reproducible by using set.seed (and maybe smaller)? If I understand correctly, you want to know if let say row 1 items from df2 (8,16) are both in item column of specific id? If I am correct in guessing, I cannot find another solution than split your df according to id x <- split(df, df$id)[[1]] and for each row of df2 test if within the specified id you can find both numbers. sum(is.element(df2[1,], x$item))==2 [1] FALSE So basically 2 cycles, one for df ids and the other for df2 rows. But maybe somebody will give you more ingenious answer. Cheers Petr> -----Original Message----- > From: R-help <r-help-bounces at r-project.org> On Behalf Of Marine > Andersson > Sent: Friday, October 7, 2022 1:58 PM > To: r-help at r-project.org > Subject: [R] Ids with matching number combinations? > > 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 > boththe> numbers occuring in the same row in the df2 dataframe? In the output Iwould> 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 KIkommer> att behandla dina personuppgifter. H?r finns information om hur KIbehandlar> personuppgifter<nam10.safelinks.protection.outlook.com/?url=https://ki.se/medarbetare/integritetsskyddspolicy&data=05|01|tebert@ufl.edu|7346e6bc695846d7264508daa86cac5a|0d4da0f84a314d76ace60a62331e1b84|0|0|638007482638899002|Unknown|TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0=|2000|||&sdata=/17yDHhLyAUZFLVC9g73jTSLvncGW89KB5SiBpMo1u8=&reserved=0>. > > > Sending email to Karolinska Institutet (KI) will result in KI > processingyour> personal data. You can read more about KI's processing of personal > data here<nam10.safelinks.protection.outlook.com/?url=https://ki.se/en/staff/data-protection-policy&data=05|01|tebert@ufl.edu|7346e6bc695846d7264508daa86cac5a|0d4da0f84a314d76ace60a62331e1b84|0|0|638007482638899002|Unknown|TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0=|2000|||&sdata=E6valRizvf2Ff5TSUUp6ut30E6D3BF+iMNDrmDOZxfs=&reserved=0>. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > nam10.safelinks.protection.outlook.com/?url=https://stat > .ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl > .edu%7C7346e6bc695846d7264508daa86cac5a%7C0d4da0f84a314d76ace60a62331e > 1b84%7C0%7C0%7C638007482638899002%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w > LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C2000%7C%7C%7C > &sdata=hKFMIJOKSUHjM7GRTn9RkTAMocHRQQwO5lB6tUMe%2FUI%3D&reserv > ed=0 > PLEASE do read the posting guidenam10.safelinks.protection.outlook.com/?url=http://www.r-project.org/posting-guide.html&data=05|01|tebert@ufl.edu|7346e6bc695846d7264508daa86cac5a|0d4da0f84a314d76ace60a62331e1b84|0|0|638007482638899002|Unknown|TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0=|2000|||&sdata=dM+vORlAYU2/0uHF9d/3sEl4GdurEGDjgk+s6QxazZQ=&reserved=0> and provide commented, minimal, self-contained, reproducible code.