[R] Efficient lookup on a two-dimensional table

David Winsemius dwinsemius at comcast.net
Thu Jun 25 16:30:14 CEST 2009


On Jun 25, 2009, at 10:24 AM, Rama Ramakrishnan wrote:

> Follow-on question: is there a way to do this for higher-dimensional  
> (i.e.
> more than 2 dimensions) arrays?

The apply method I just posted generalizes to higher dimensional arrays.

-- 
DW

>
>
> On Thu, Jun 25, 2009 at 10:17 AM, Rama Ramakrishnan  
> <rama at alum.mit.edu>wrote:
>
>> That works!! Very nice way to do it! Thank you, Henrique!
>> Rama Ramakrishnan
>>
>>
>> On Thu, Jun 25, 2009 at 10:11 AM, Henrique Dallazuanna <wwwhsd at gmail.com 
>> >wrote:
>>
>>> Try this:
>>>
>>> y$values <- diag(x[y$ltrs, y$mnths])
>>>
>>> On Thu, Jun 25, 2009 at 11:02 AM, Rama Ramakrishnan <rama at alum.mit.edu 
>>> >wrote:
>>>
>>>> Dear R-Users,
>>>> I need to lookup values from a 2-d table using the row names and  
>>>> column
>>>> names as indices. I was wondering if there's a way to do this  
>>>> without an
>>>> explicit loop.
>>>>
>>>> Example:
>>>> #x is the 2-d table that holds the values
>>>>
>>>> x <- matrix(rnorm(26*12),nrow=26)
>>>>
>>>> rownames(x) <- letters
>>>>
>>>> colnames(x) <- month.name
>>>>
>>>>
>>>> #y is a data frame that has the "keys" I want to use as indices  
>>>> into x
>>>>
>>>> y <- data.frame(ltrs=sample(letters,5),mnths=sample(month.name
>>>> ,5),values=0)
>>>>
>>>>
>>>> #I want to fill in the "values" column using the "ltrs" and "mnths"
>>>> columns
>>>> as keys to look up
>>>>
>>> snip
>>
>>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list