<font color="#993399"><font size="4"><font face="comic sans ms,sans-serif">Prezados, preciso trabalhar com os pares resultantes do RecordLinkage e não sei como fazer. Enviei email para o autor do pacote, mas ele disse que terei que programar, pois não existe uma maneira automática de fazer isso. Bem, aí vai, meu desafio:</font></font></font><div>
<font color="#993399"><font size="4"><font face="comic sans ms,sans-serif">Tenho dois bancos ( um retirado do outro) e vou fazer o linkage para obter as readmissões. No resultado sempre haverá um par de cada um, com mesma data de internação, já que o banco menor é retirado do maior (o menor é de internados por fratura de fêmur e o maior é de internados por todas as causas). Aí vai um exemplo criado para você verem como é a saída dos pares:</font></font></font></div>
<div><div><br></div><div><div><font face="arial, helvetica, sans-serif">nome=c("Ricardo","Maria","Tereza","Pedro","José", "Germano")</font></div><div><font face="arial, helvetica, sans-serif">sobrenome=c("Cunha","Andrade","Silva","Soares","Silva","Lima")</font></div>
<div><font face="arial, helvetica, sans-serif">idade=c(67,89,78,65,68,67)</font></div><div><font face="arial, helvetica, sans-serif">ano=c(1945,1923,1934,1947,1944,1945)</font></div><div><font face="arial, helvetica, sans-serif">dataint=c(20120907,20120703,20120301,20120805,20121004,20121209)</font></div>
<div><font face="arial, helvetica, sans-serif">banco1=cbind(nome,sobrenome,idade,ano,dataint)</font></div><div><font face="arial, helvetica, sans-serif">banco1=data.frame(banco1)</font></div><div><font face="arial, helvetica, sans-serif">banco1</font></div>
<div><font face="arial, helvetica, sans-serif">nome=c("Maria","Lúcia","Paulo","Marcos", "Ricardo", "Germanio")</font></div><div><font face="arial, helvetica, sans-serif">sobrenome=c("Andrade","Silva","Soares","Pereira","Cunha","Lima")</font></div>
<div><font face="arial, helvetica, sans-serif">idade=c(67,88,78,60,68,80)</font></div><div><font face="arial, helvetica, sans-serif">ano=c(1945,1924,1934,1952,1944,1932)</font></div><div><font face="arial, helvetica, sans-serif">dataint=c(20121208,20121103,20120302,20120105,20121004,20121209)</font></div>
<div><font face="arial, helvetica, sans-serif">banco2=cbind(nome,sobrenome,idade,ano,dataint)</font></div><div><font face="arial, helvetica, sans-serif">banco2=data.frame(banco2)</font></div><div><font face="arial, helvetica, sans-serif">banco2</font></div>
<div><font face="arial, helvetica, sans-serif">str(banco1)</font></div><div><font face="arial, helvetica, sans-serif">banco1$nome=as.character(banco1$nome)</font></div><div><font face="arial, helvetica, sans-serif">banco1$sobrenome=as.character(banco1$sobrenome)</font></div>
<div><font face="arial, helvetica, sans-serif">banco1$idade=as.character(banco1$idade)</font></div><div><font face="arial, helvetica, sans-serif">banco1$ano=as.character(banco1$ano)</font></div><div><font face="arial, helvetica, sans-serif">banco1$dataint=as.character(banco1$dataint)</font></div>
<div><font face="arial, helvetica, sans-serif">banco2$nome=as.character(banco2$nome)</font></div><div><font face="arial, helvetica, sans-serif">banco2$sobrenome=as.character(banco2$sobrenome)</font></div><div><font face="arial, helvetica, sans-serif">banco2$idade=as.character(banco2$idade)</font></div>
<div><font face="arial, helvetica, sans-serif">banco2$ano=as.character(banco2$ano)</font></div><div><font face="arial, helvetica, sans-serif">banco2$dataint=as.character(banco2$dataint)</font></div><div><font face="arial, helvetica, sans-serif">library(RecordLinkage)</font></div>
<div><font face="arial, helvetica, sans-serif">soundexBR<-function(termo){</font></div><div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>termo<-toupper(termo)</font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span># 1. Retire toda pontuação da palavra;</font></div><div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>termo<-gsub("[ÃÁÀÂÄ]","A",termo)</font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>termo<-gsub("[ÉÈÊ?Ë]","E",termo)</font></div><div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>termo<-gsub("[ÍÌÏÎI]","I",termo)</font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>termo<-gsub("[ÓÕÒÔÖ]","O",termo)</font></div><div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>termo<-gsub("[ÚÙÛÜU]","U",termo)</font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>termo<-gsub("Ç","C",termo)</font></div><div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>#1.a Substituir todas as letras duplas por uma única letra</font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>#<a href="http://www.archives.gov/genealogy/census/soundex.html">http://www.archives.gov/genealogy/census/soundex.html</a></font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>termo<-gsub("([A-Z])\\1", "\\1", termo)</font></div><div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span># 2. Fique com a primeira letra;</font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span> N<-nchar(termo)</font></div><div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span># Modificação PT-BR (Coeli e Camargo)</font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span> termo<-ifelse(substr(termo,1,2)=="WA",sub("W","V",termo),termo)</font></div><div>
<font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span> termo<-ifelse(substr(termo,1,1)=="H",substr(termo,2,N),termo)</font></div><div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span> termo<-ifelse(substr(termo,1,2)=="KA"|substr(termo,1,2)=="KO"|substr(termo,1,2)=="KU",sub("K","C",termo),termo)</font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span> termo<-ifelse(substr(termo,1,1)=="Y",sub("Y","I",termo),termo)</font></div><div>
<font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span> termo<-ifelse(substr(termo,1,2)=="CE"|substr(termo,1,2)=="CI",sub("C","S",termo),termo)</font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span> termo<-ifelse(substr(termo,1,2)=="GE"|substr(termo,1,2)=="GI",sub("G","J",termo),termo)<span class="Apple-tab-span" style="white-space:pre"> </span> </font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>############## Segunda Parte #######################<span class="Apple-tab-span" style="white-space:pre"> </span> </font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span> termo.1<-substr(termo,1,1)</font></div><div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span> termo<-substr(termo,2,N)</font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span># 3. Mude todas as ocorrências das letras a seguir por '0' (zero): 'A','E','I','O','U','H','W','Y';</font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span> termo<-gsub("[A,E,I,O,U,H,W,Y]",0,termo)</font></div><div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span># 4. Mude as letras restantes de acordo com a tabela abaixo: </font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span># Número<span class="Apple-tab-span" style="white-space:pre"> </span>Letra </font></div><div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span># 1<span class="Apple-tab-span" style="white-space:pre"> </span>'B','F','P','V'</font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span> termo<-gsub("[B,F,P,V]",1,termo)</font></div><div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span># 2<span class="Apple-tab-span" style="white-space:pre"> </span>'C','G','J','K', 'Q','S','X','Z'</font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span> termo<-gsub("[C,G,J,K,Q,S,X,Z]",2,termo)</font></div><div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span># 3<span class="Apple-tab-span" style="white-space:pre"> </span>'D','T'</font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span> termo<-gsub("[D,T]",3,termo) </font></div><div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span># 4<span class="Apple-tab-span" style="white-space:pre"> </span>'L' </font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span> termo<-gsub("L",4,termo)</font></div><div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span># 5<span class="Apple-tab-span" style="white-space:pre"> </span>'M','N' </font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span> termo<-gsub("[M,N]",5,termo)</font></div><div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span># 6<span class="Apple-tab-span" style="white-space:pre"> </span>'R'</font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span> termo<-gsub("R",6,termo)</font></div><div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span># 5. Remova todos os zeros da string resultante;</font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span> termo<-gsub(0,"",termo)</font></div><div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span># Eliminar dois numeros iguais consecutivos por um único numero </font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>#<a href="http://www.archives.gov/genealogy/census/soundex.html">http://www.archives.gov/genealogy/census/soundex.html</a></font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span> termo<-gsub("([0-9])\\1", "\\1", termo) </font></div><div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span># Remontar </font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span> termo<-paste(termo.1,termo,sep="")</font></div><div><font face="arial, helvetica, sans-serif"> </font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span># 6. Preencha a string resultante com zeros à direita e retorne desta forma: </font></div><div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>#<letra maiuscula><digito><digito><digito></font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span> termo<-paste(termo,"0000",sep="")</font></div><div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span> termo<-substr(termo,1,4)</font></div>
<div><font face="arial, helvetica, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span> return(termo)</font></div><div><font face="arial, helvetica, sans-serif">} </font></div><div><font face="arial, helvetica, sans-serif"><br>
</font></div><div><font face="arial, helvetica, sans-serif">soundexBR("termo")</font></div><div><font face="arial, helvetica, sans-serif">soundexBR("terno")</font></div><div><font face="arial, helvetica, sans-serif"> </font></div>
<div><font face="arial, helvetica, sans-serif">pares=compare.linkage(banco1,banco2,blockfld=list(c(1,2,4),c(1,2)),</font></div><div><font face="arial, helvetica, sans-serif"> phonetic=c(1,2),phonfun=soundexBR,strcmp=FALSE,</font></div>
<div><font face="arial, helvetica, sans-serif"> strcmpfun=jarowinkler, exclude=FALSE,identity1=NA,identity2=NA,</font></div><div><font face="arial, helvetica, sans-serif"> n_match=NA, n_non_match=NA)</font></div>
<div><font face="arial, helvetica, sans-serif">editMatch(pares)</font></div><div><font face="arial, helvetica, sans-serif">pares</font></div><div><span style="font-family:arial,helvetica,sans-serif">pesos=epiWeights(pares, e=0.01,f=pares$frequencies)</span></div>
<div><font face="arial, helvetica, sans-serif">hist(pesos$Wdata,plot=F)</font></div><div><font face="arial, helvetica, sans-serif">getPairs(pesos,max.weight=Inf, min.weight=-Inf)</font></div><div><br></div></div><div style="color:rgb(153,51,153);font-size:large;font-family:'comic sans ms',sans-serif">
Agora, como tenho as datas da internação de todos, preciso verificar, par a par, quem tem data de internação diferente. Estes serão as readmissões. Existe uma forma de eu criar uma coluna para identificar os pares? E depois, testar se as datas de internação são diferentes?</div>
</div><div style="color:rgb(153,51,153);font-size:large;font-family:'comic sans ms',sans-serif">Obrigada</div><div style="color:rgb(153,51,153);font-size:large;font-family:'comic sans ms',sans-serif">Fátima</div>