# Selecting rows that are the same in separate data frames

 Classic List Threaded
5 messages
Reply | Threaded
Open this post in threaded view
|

## Selecting rows that are the same in separate data frames

 I want to compare two matrices or data frames and select or get an index for those rows which are the same in both. I have tried the following : a = matrix ( 1:10, ncol = 2 ) a b = matrix ( c ( 2,3,4,7,8,9 ), ncol = 2 ) b a[a==b] a = as.data.frame ( matrix ( 1:10, ncol = 2 ) ) a b = as.data.frame ( matrix ( c ( 2,3,4,7,8,9 ), ncol = 2 ) ) b a[a==b] Any ideas please. Thanks. Simon Parker Imperial College
Reply | Threaded
Open this post in threaded view
|

## Re: Selecting rows that are the same in separate data frames

 I'm not sure what you want, but take a look at ?merge and %in% ppaarrkk wrote I want to compare two matrices or data frames and select or get an index for those rows which are the same in both. I have tried the following : a = matrix ( 1:10, ncol = 2 ) a b = matrix ( c ( 2,3,4,7,8,9 ), ncol = 2 ) b a[a==b] a = as.data.frame ( matrix ( 1:10, ncol = 2 ) ) a b = as.data.frame ( matrix ( c ( 2,3,4,7,8,9 ), ncol = 2 ) ) b a[a==b] Any ideas please. Thanks. Simon Parker Imperial College
Reply | Threaded
Open this post in threaded view
|

## Re: Selecting rows that are the same in separate data frames

 In reply to this post by ppaarrkk Dear Simon, Try this: # Index -- FALSE, TRUE sapply(1:nrow(a),function(x) all(a[x,]%in%b)) #  Rows of a that are in b which(sapply(1:nrow(a),function(x) all(a[x,]%in%b))) # Reporting a[sapply(1:nrow(a),function(x) all(a[x,]%in%b)),] HTH, Jorge On Tue, Dec 9, 2008 at 9:57 AM, ppaarrkk <[hidden email]> wrote: > > I want to compare two matrices or data frames and select or get an index > for > those rows which are the same in both. I have tried the following : > > > > > > > a = matrix ( 1:10, ncol = 2 ) > a > > b = matrix ( c ( 2,3,4,7,8,9 ), ncol = 2 ) > b > > a[a==b] > > > > > > > a = as.data.frame ( matrix ( 1:10, ncol = 2 ) ) > a > > b = as.data.frame ( matrix ( c ( 2,3,4,7,8,9 ), ncol = 2 ) ) > b > > a[a==b] > > > > > > > > > Any ideas please. > > > Thanks. > > > Simon Parker > Imperial College > > -- > View this message in context: > http://www.nabble.com/Selecting-rows-that-are-the-same-in-separate-data-frames-tp20916243p20916243.html> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. >         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: Selecting rows that are the same in separate data frames

 In reply to this post by ppaarrkk Try this: 1) Rows: merge(as.data.frame(a), as.data.frame(b), sort = FALSE) 2) Index: merge(cbind(as.data.frame(a), Idx = 1:nrow(a)), as.data.frame(b), sort = FALSE)\$Idx On Tue, Dec 9, 2008 at 12:57 PM, ppaarrkk <[hidden email]> wrote: > > I want to compare two matrices or data frames and select or get an index > for > those rows which are the same in both. I have tried the following : > > > > > > > a = matrix ( 1:10, ncol = 2 ) > a > > b = matrix ( c ( 2,3,4,7,8,9 ), ncol = 2 ) > b > > a[a==b] > > > > > > > a = as.data.frame ( matrix ( 1:10, ncol = 2 ) ) > a > > b = as.data.frame ( matrix ( c ( 2,3,4,7,8,9 ), ncol = 2 ) ) > b > > a[a==b] > > > > > > > > > Any ideas please. > > > Thanks. > > > Simon Parker > Imperial College > > -- > View this message in context: > http://www.nabble.com/Selecting-rows-that-are-the-same-in-separate-data-frames-tp20916243p20916243.html> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. > -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: Selecting rows that are the same in separate data frames

 In reply to this post by Bart Joosen Thanks for reply. What I want is the equivalent of this : xxx = 1:10 which(xxx %in% c(2,5)) .......but where there is more than one criterion for matching. which (b %in% a) in the code I included does nothing (not surprisingly). I'm not sure that I can use merge, because I want the whole of a, but to mark those rows which are also in b. If I do merge ( a,b ),  I just get b. If I do merge ( a,b, all.x =TRUE), I get a. bartjoosen wrote I'm not sure what you want, but take a look at ?merge and %in% ppaarrkk wrote I want to compare two matrices or data frames and select or get an index for those rows which are the same in both. I have tried the following : a = matrix ( 1:10, ncol = 2 ) a b = matrix ( c ( 2,3,4,7,8,9 ), ncol = 2 ) b a[a==b] a = as.data.frame ( matrix ( 1:10, ncol = 2 ) ) a b = as.data.frame ( matrix ( c ( 2,3,4,7,8,9 ), ncol = 2 ) ) b a[a==b] Any ideas please. Thanks. Simon Parker Imperial College