My question is probably pretty basic, but since I'm really new to R, here it goes .... I have two separate data frames that include class names and various other information on classes. I'm trying to create a match based on class names and if a match exists to create a third data frame with the class name. I was hoping to accomplish this with sapply, but I can't figure out if I can embed an "if statement" with sapply. Does anyone know if that's a legit way to accomplish what I am trying to do or is there some better way? thank you, Alina Sheyman [[alternative HTML version deleted]]
I'm not sure I understand what you're looking for in the result. What exactly do you mean by a "match"? What do you want in the third table besides the class names? Do you just want a list (not a data frame) of those class names which table A and table B have in common? Then how about intersect(A$class,B$class)? If you want to do something like a SQL join of tables A and B, where rows are paired up based on common values of the class name, then take a look at the "merge" function. -s On Mon, Nov 17, 2008 at 3:04 PM, Alina Sheyman <alinashe at gmail.com> wrote:> My question is probably pretty basic, but since I'm really new to R, here it > goes .... > > I have two separate data frames that include class names and various other > information on classes. I'm trying to create a match based on class names > and if a match exists to create a third data frame with the class name. > > I was hoping to accomplish this with sapply, but I can't figure out if I can > embed an "if statement" with sapply. Does anyone know if that's a legit way > to accomplish what I am trying to do or is there some better way? > > thank you, > > Alina Sheyman > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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. >
Alina I suspect you want match (or %in%): ?match HTH .... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Alina Sheyman > Sent: Tuesday, 18 November 2008 9:04 a.m. > To: r-help at r-project.org > Subject: [R] looking for matches > > My question is probably pretty basic, but since I'm really > new to R, here it goes .... > > I have two separate data frames that include class names and > various other information on classes. I'm trying to create a > match based on class names and if a match exists to create a > third data frame with the class name. > > I was hoping to accomplish this with sapply, but I can't > figure out if I can embed an "if statement" with sapply. Does > anyone know if that's a legit way to accomplish what I am > trying to do or is there some better way? > > thank you, > > Alina Sheyman > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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. >The contents of this e-mail are privileged and/or confidential to the named recipient and are not to be used by any other person and/or organisation. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail.