[R] help matching rows of a data frame

William Dunlap wdunlap at tibco.com
Mon Sep 18 17:26:23 CEST 2017

You could use merge() with an ID column pasted onto the table of names, as

> tbl <- data.frame(FirstName=c("Abe","Abe","Bob","Chuck","Chuck"),
Surname=c("Xavier","Yates","Yates","Yates","Zapf"), Id=paste0("P",101:105))
> tbl
  FirstName Surname   Id
1       Abe  Xavier P101
2       Abe   Yates P102
3       Bob   Yates P103
4     Chuck   Yates P104
5     Chuck    Zapf P105
> merge(data.frame(FirstName=c("Abe","Chuck","Dave"),
Surname=rep("Yates",3)), tbl, all.x=TRUE)
  FirstName Surname   Id
1       Abe   Yates P102
2     Chuck   Yates P104
3      Dave   Yates <NA>

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Mon, Sep 18, 2017 at 5:13 AM, Therneau, Terry M., Ph.D. <
therneau at mayo.edu> wrote:

> This question likely has a 1 line answer, I'm just not seeing it.  (2, 3,
> or 10 lines is fine too.)
> For a vector I can do group  <- match(x, unqiue(x)) to get a vector that
> labels each element of x.
> What is an equivalent if x is a data frame?
> The result does not have to be fast: the data set will have < 100
> elements.  Since this is inside the survival package, and that package is
> on  the 'recommended' list, I can't depend on any package outside the
> recommended list.
> Terry T.
> ______________________________________________
> R-help at 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/posti
> ng-guide.html
> and provide commented, minimal, self-contained, reproducible code.

	[[alternative HTML version deleted]]

More information about the R-help mailing list