[R] matching each row

tathta caitlyn.paget at gmail.com
Wed Jul 8 19:17:07 CEST 2009


>From an email suggestion, here are two sample datasets, and my ideal output: 

dataA <- data.frame(unique.id=c("A","B","C","B"),x=11:14,y=5:2)
dataB <-
data.frame(unique.id=c("A","B","A","B","A","C","D","A"),x=27:20,y=22:29)

## mystery operation(s) happen here....

## ideal output would be: 
dataA <-
data.frame(unique.id=c("A","B","C","B"),x=11:14,y=5:2,countA=c(1,2,1,2),countB=c(4,2,1,2))


so my mystery operation(s) would count the number of times the unique id
shows up in a given dataset.  
my ideal outputs are as follows: 
countA is the "mystery operation" applied to dataA (counting occurrences
within the same dataset)
countB is applied to dataB (counting occurrences within a second dataset).  



My best try so far is to do: 
tempA <- aggregate(dataA$unique.id,list(dataA$unique.id),length)

which gives me a matrix with ONE instance of each unique.id and the
counts...
(and which I thought was kinda cute)
but it only works for within a single dataset! 




tathta wrote:
> 
> 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!
> 
> 
> 
> thx
> 

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




More information about the R-help mailing list