[R] simple loop questions

David Winsemius dwinsemius at comcast.net
Sun Apr 24 18:05:02 CEST 2011


On Apr 23, 2011, at 9:34 AM, David Winsemius wrote:

>
> On Apr 23, 2011, at 6:31 AM, <xueke at pdx.edu> wrote:
>
>> Hi all,
>>
>> I am trying to write a loop to return the matched index for a  
>> column of values. Here is an simple example of this,
>>
>> A   B
>> 0   5
>> 1   2
>> 2   2
>> 3   4
>> 4   1
>> 5   4
>> 6   2
>>
>> In this case, A is an index column for B. Now I have this new  
>> column C with just two values of 2 and 4. I want to match column C  
>> with column B, and return the matched indexes. So what I desire is  
>> to return:
>>
>> [1] 1 2 6
>> [2] 3 5
>>
>> Since value 2 corresponds to indexes 1,2,6, and 4 corresponds to  
>> indexes 3,5.
>
> Assuming this to be in a data.frame named dat:
>
> > dat$A[which(dat$B==2) ]
> [1] 1 2 6

I've been reminded that in many instances one gets comparable results  
with dat$A[ dat$B==2 ] (logical indexing).

The reason I choose to use which() is that it returns a numeric vector  
and leaves out any NA's that result from dat$B==2. the "[" function  
returns all logical NA rows. It may be important to return such NA's  
to alert the analyst to their presence. (This does result in extensive/ 
useless/unexpected screen output when large datasets with even a small  
faction of NA's are being manipulated.)

-- 
David

> > dat$A[which(dat$B==4) ]
> [1] 3 5


>
> Results of varying lengths generally need to be returned in list form:
>
> > sapply(c(2,4), function(x) dat$A[which(dat$B==x) ] )
> [[1]]
> [1] 1 2 6
>
> [[2]]
> [1] 3 5
>
>>

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list