[R] indexing question

Adrian Dusa adi at roda.ro
Wed May 19 17:43:47 CEST 2004


Hi,

I have a problem and a rather poor solution that I would like to improve.
There a 2 datasets with different number of cases like this:

Dataset 'poploc’                                Dataset 'siruta’

Case no.     SIRUTA     TYPE                    Case no.     SIRUTA     TYPE
1            1017        0                      1            1017        3  
2            1026        0                      2            1020        5
3            42711       0                      3            1026        4
....                                               ....
13000        100234      0                      ....
                                                16000        160241      3

I want to bring the TIP variable in the 'poploc’ dataset according to the 
SIRUTA variable (which has unique codes for each case, in both datasests).
The resulting dataset 'poploc' should look like this:

Case no.     SIRUTA     TYPE
1            1017        3
2            1026        4
3            42711       3
....
13000        100234      5

My current solution involves a combination of FOR looping and indexing, which 
takes about 3 minutes to complete.

for (i in 1:nrow(siruta))
poploc$TIP[poploc$SIRUTA %in% siruta$SIRUTA[i]] <- siruta$TIP[i]

I’m sure there are more clever solutions, any help appreciated. Thank you!
Adrian




More information about the R-help mailing list