[R] indexing question

Sundar Dorai-Raj sundar.dorai-raj at PDF.COM
Wed May 19 17:53:57 CEST 2004



Adrian Dusa wrote:

> 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
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

I think you are looking for ?merge.

# note that poploc has no `TYPE' column
poploc <- data.frame(no = 1:3, SIRUTA = c(1017, 1026, 42711))
siruta <- data.frame(no = c(1:3, 16000),
                      SIRUTA = c(1017, 1026, 42711, 160241),
                      TYPE = c(3, 5, 4, 3))
merge(poploc, siruta)




More information about the R-help mailing list