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
take a look at match or maybe merge>-----Original Message----- >From: Adrian Dusa [mailto:adi at roda.ro] >Sent: Wednesday, May 19, 2004 8:44 AM >To: r-help at stat.math.ethz.ch >Subject: [R] indexing question > > >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
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.htmlI 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)
Maybe Matching Threads
- [LLVMdev] teaching FileCheck to handle variations in order
- [LLVMdev] teaching FileCheck to handle variations in order
- [LLVMdev] teaching FileCheck to handle variations in order
- [bug report] drm/nouveau/gsp/r535: add support for booting GSP-RM
- Plot unusual subset of data