# indexing via a character matrix?

4 messages
Open this post in threaded view
|

## indexing via a character matrix?

 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 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Open this post in threaded view
|

## Re: indexing via a character matrix?

 On Mon, 13 Feb 2006, Roger Levy wrote: > 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 Numeric or logical. > character tuples out of a contingency table without resorting to writing > loops? 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. -- Brian D. Ripley,                  [hidden email] Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/University of Oxford,             Tel:  +44 1865 272861 (self) 1 South Parks Road,                     +44 1865 272866 (PA) Oxford OX1 3TG, UK                Fax:  +44 1865 272595 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide! http://www.R-project.org/posting-guide.html