[R] matching each row

David Huffer David.Huffer at csosa.gov
Wed Jul 8 19:23:29 CEST 2009


Something like this? 

  > dataframeA <- data.frame (
  +   unique.id= c(1,1,3,3,3,5,7,7, 9)
  +   , x1=rnorm(9)
  +   , x2=rnorm(9)
  +   , x3=rnorm(9)
  + )
  > dataframeB <- data.frame (
  +   unique.id= c(2,3,4,5,5,5,6,7,9,10,10)
  +   , x4=rnorm(11)
  +   , x5=rnorm(11)
  +   , x6=rnorm(11)
  + )
  > match.counts <- function ( x , y ) {
  +   out <- cbind (
  +     table ( x [ which ( x %in% y ) ] )
  +     , table ( y [ which ( y %in% x ) ] )
  +   )
  +   dimnames ( out ) [[2]] <- c ( "N in x" , "N in y" )
  +   out
  + }
  > match.counts ( dataframeA$unique.id , dataframeB$unique.id )
    N in x N in y
  3      3      1
  5      1      3
  7      2      1
  9      1      1
  >

--
 David
 
 -----------------------------------------------------
 David Huffer, Ph.D.               Senior Statistician
 CSOSA/Washington, DC           david.huffer at csosa.gov
 -----------------------------------------------------

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of tathta
Sent: Wednesday, July 08, 2009 11:10 AM
To: r-help at r-project.org
Subject: [R] matching each row


I have two dataframes, the first column of each dataframe is a unique id
number (the rest of the columns are data variables).  
I would like to figure out how many times each id number appears in each
dataframe.  

So far I can use: 
length( match (dataframeA$unique.id[1], dataframeB$unique.id) )

but this only works on each row of dataframe A one-at-a-time.  

I would like to do this for all of the rows in dataframe A, and then put the
results in a new variable: dataframeA$count


I'm new to R, so please be patient with me!


Sorry if this question has already been answered, my search of the archives
only brought up one relevant post, and I didn't understand the answer to
it....  http://www.nabble.com/match-to20799206.html#a20799206


thx
-- 
View this message in context: http://www.nabble.com/matching-each-row-tp24393051p24393051.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
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.




More information about the R-help mailing list