Skip to content

indexing via a character matrix?

4 messages · Roger Levy, Brian Ripley

#
Hi,

Is it possible to index via a character matrix?  For example, I would 
like to do the following:

cont.table <- table(df1$A,df1$B) # df1 a data frame with factors A and B
cont.table[cbind(df2$A,df2$B)]   # df2 a smaller data frame with some
                                  # pairings of values for A and B

but the second step does not work -- I guess that matrices to be used 
for indexing this way must be numeric.  Is there a way to index multiple 
character tuples out of a contingency table without resorting to writing 
loops?

Many thanks,

Roger Levy
#
On Mon, 13 Feb 2006, Roger Levy wrote:

            
Numeric or logical.
What are you trying to do here?  One possibility is that you meant

comt.table[df2$A, df2$B]

and another is

ind1 <- match(df2$A, df1$A)
ind2 <- match(df2$B, df1$B)
cont.table[cbind(ind1, ind2)]

and I am not certain which.
#
Prof Brian Ripley wrote:
Hmm, I think neither one is what I wanted.  After thinking more about 
it, I think what I would need is

ind1 <- match(df2$A,levels(df1$A))
ind2 <- match(df2$B,levels(df2$B))
cont.table[cbind(ind1,ind2)]

Does this make sense?  Is there a more natural way to express this?

Many thanks,

Roger
#
On Mon, 13 Feb 2006, Roger Levy wrote:

            
Not really.

You said character, but you seem to have a factor (by the use of levels). 
It is the case that cont.table will have dims indexed by the levels of 
df1$A and df1$B, so perhaps you want

ind1 <- match(df2$A,levels(df1$A))
ind2 <- match(df2$B,levels(df1$B))
cont.table[cbind(ind1,ind2)]

That gives you a vector along rows of df2 giving the count of that A,B
combination in df1 (provide all such occur).  If some might not occur,
use matchdf2$A,levels(df1$A), NA) etc.