<html><head></head><body><div class="ydp9b0706abyahoo-style-wrap" style="font-family: bookman old style, new york, times, serif; font-size: 16px;"><div dir="ltr" data-setdir="false" style="font-size: 16px;">Prezados, bom dia de novo, lá vou perturbar os amigos... Segue outro exemplo reproduzível (tem a ver com minha dúvida de ontem):</div><div dir="ltr" data-setdir="false" style="font-size: 16px;"><br></div><div dir="ltr" data-setdir="false" style=""><div style=""><div style=""><span style="color: rgb(128, 128, 128); font-size: small;">library(dplyr)</span><br></div><div style=""><span style="color: rgb(128, 128, 128); font-size: small;">x01<-c(1001,"Potássio",3.49,3.62,3.63,3.49,3.44,2)</span><br></div><div style=""><font color="#808080" size="2">x02<-c(2000,"Potássio",4.18,4.21,4.15,4.86,4.36,3)</font></div><div style=""><font color="#808080" size="2">x03<-c(1002,"Potássio",3.49,3.62,3.63,3.49,3.44,2)</font></div><div style=""><font color="#808080" size="2">x04<-c(1003,"Potássio",3.49,3.62,3.63,3.49,3.44,2)</font></div><div style=""><font color="#808080" size="2">x05<-c(2001,"Potássio",4.18,4.21,4.15,4.86,4.36,3)</font></div><div style=""><font color="#808080" size="2">x06<-c(2000,"Albumina",2.35,2.36,2.21,2.15,2.27,10)</font></div><div style=""><font color="#808080" size="2">x07<-c(3000,"Albumina",2.69,2.78,2.88,2.91,2.84,12)</font></div><div style=""><font color="#808080" size="2">x08<-c(2001,"Albumina",2.35,2.36,2.21,2.15,2.27,10)</font></div><div style=""><font color="#808080" size="2">x09<-c(3001,"Albumina",2.69,2.78,2.88,2.91,2.84,12)</font></div><div style=""><font color="#808080" size="2"><br></font></div><div style=""><font color="#808080" size="2">exemplo<-data.frame(rbind(x01,x02,x03,x04,x05,x06,x07,x08,x09))</font></div><div style=""><font color="#808080" size="2">names(exemplo)<-c("CodCli","Substância","v1","v2","v3","v4","v5","Cluster")</font></div><div style=""><font color="#808080" size="2">row.names(exemplo)<-NULL</font></div><div style="font-size: 16px;"><br></div><div style="" dir="ltr" data-setdir="false"><div style=""><div style="" dir="ltr" data-setdir="false"><font color="#808080" style="" size="2"><div><div>exemplo<-exemplo %>%</div><div>mutate(v1=as.numeric(v1),v2=as.numeric(v2),v3=as.numeric(v3),v4=as.numeric(v4),</div><div>       v5=as.numeric(v5))</div></div></font></div><div style=""><span style="color: rgb(128, 128, 128); font-size: small;"><br></span></div><div style=""><span style="color: rgb(128, 128, 128); font-size: small;">exemplo$Média<-rowMeans(exemplo[,3:7],na.rm = TRUE)</span><br></div><div style="font-size: 16px;"><br style="color: rgb(0, 0, 0); font-family: bookman old style, new york, times, serif;"></div></div></div><div dir="ltr" data-setdir="false" style="font-size: 16px;">Como o de ontem, esse banco tem variáveis que identificam o código do cliente, a média dos dados (iguais entre laboratórios) e o Cluster, que foi obtido previamente. A substância é detalhe adicional.</div><div dir="ltr" data-setdir="false" style="font-size: 16px;"><br></div><div dir="ltr" data-setdir="false" style="font-size: 16px;">O que eu gostaria é: reestruturar esse banco, colocando como variáveis de identificação a substância, o cluster e a média, e o código do cliente indo para as colunas, como está nessa tabela abaixo:</div><div style="font-size: 16px;"><br></div><div style="font-size: 16px;" dir="ltr" data-setdir="false"><b style=""><span style="background-color: rgb(253, 248, 105);">Subst       Cluster    Média       CodCli.1   </span><span style="color: rgb(0, 0, 0); font-family: bookman old style, new york, times, serif; font-size: 16px;"><span style="background-color: rgb(253, 248, 105);">CodCli.2   <span style="color: rgb(0, 0, 0); font-family: bookman old style, new york, times, serif; font-size: 16px;">CodCli.3</span></span></span></b></div><div style="font-size: 16px;" dir="ltr" data-setdir="false"><div><div><span style="background-color: rgb(253, 248, 105);">Albumina<span style="white-space: pre-wrap;">        </span>10<span style="white-space: pre-wrap;">            </span>2,2680<span style="white-space: pre-wrap;">          </span>2000<span style="white-space: pre-wrap;">           </span>2001<span style="white-space: pre-wrap;">       </span></span></div><div><span style="background-color: rgb(253, 248, 105);">Albumina<span style="white-space: pre-wrap;">      </span>12<span style="white-space: pre-wrap;">            </span>2,8200   <span style="white-space: pre-wrap;">    </span>3000<span style="white-space: pre-wrap;">    </span>3001<span style="white-space: pre-wrap;">       </span></span></div><div><span style="background-color: rgb(253, 248, 105);">Potássio<span style="white-space: pre-wrap;">     </span>2<span style="white-space: pre-wrap;">             </span>3,5340<span style="white-space: pre-wrap;">          </span>1001<span style="white-space: pre-wrap;">           </span>1002<span style="white-space: pre-wrap;">           </span>1003</span></div><div><span style="background-color: rgb(253, 248, 105);">Potássio<span style="white-space: pre-wrap;">     </span>3<span style="white-space: pre-wrap;">             </span>4,3520<span style="white-space: pre-wrap;">          </span>2000<span style="white-space: pre-wrap;">           </span>2001<span style="white-space: pre-wrap;">       </span></span></div></div><br></div><div style="font-size: 16px;" dir="ltr" data-setdir="false"><div><div dir="ltr" data-setdir="false">Eu tentei usar o comando <font color="#cd232c">reshape</font>, com esse código abaixo, mas ele não funcionou comigo (tentei também o <font color="#cd232c">spread</font>, do tidyr): <br></div></div><br></div><div style=""><font color="#808080" style="" size="2">exemplo<-exemplo %>%<br></font></div><div style=""><font color="#808080" size="2">  select(CodCli,Substância,Cluster,Média)</font></div><div style=""><font color="#808080" size="2"><br></font></div><div style=""><font color="#808080" size="2">exemplo_quebra<-reshape(exemplo,<br></font></div><div style=""><font color="#808080" size="2">                                        direction="wide",</font></div><div style=""><font color="#808080" size="2">                                        idvar=c("Substância","Cluster","Média"),</font></div><div style=""><font color="#808080" size="2">                                        timevar="Cod.Cli")</font></div><div style="font-size: 16px;"><br></div><div style="font-size: 16px;" dir="ltr" data-setdir="false">Algum amigo poderia indicar onde estou errando? Ou se teria uma rotina melhor para realizar esse procedimento?<br></div><div style="font-size: 16px;" dir="ltr" data-setdir="false"><br></div><div style="font-size: 16px;" dir="ltr" data-setdir="false">Novamente obrigado!!!</div><div style="font-size: 16px;"><br></div></div></div><div style="font-size: 16px;"><i id="ydpcf278505yui_3_16_0_1_1497025264182_38508" style="font-family: Helvetica, Arial, sans-serif;"><span id="ydpcf278505yui_3_16_0_1_1497025264182_38507" style="color:rgb(0, 0, 191);font-size:medium;"><font face="Courier New, courier, monaco, monospace, sans-serif" style="background-color: inherit;">Diogo Jerônimo</font></span></i><br></div><div class="ydp9b0706absignature" style="font-size: 16px;"><div style="font-family:Helvetica, Arial, sans-serif;font-size:16px;"><div style="margin-bottom:0.0001pt;padding:0px;line-height:normal;background:white;"><i id="ydpcf278505yui_3_16_0_1_1497025264182_38430"><span id="ydpcf278505yui_3_16_0_1_1497025264182_38429" style="color:rgb(0, 0, 191);font-size:medium;"><font face="Courier New, courier, monaco, monospace, sans-serif" style="background-color: inherit;">Bacharel em Ciências Estatísticas - ENCE/IBGE</font></span></i></div><div style="margin-bottom:0.0001pt;padding:0px;line-height:normal;background:white;"><i id="ydpcf278505yui_3_16_0_1_1497025264182_38309"><span id="ydpcf278505yui_3_16_0_1_1497025264182_38308" style="color:rgb(0, 0, 191);font-size:medium;"><font face="Courier New, courier, monaco, monospace, sans-serif" style="background-color: inherit;">Mestre em Metrologia - PUC-Rio/PósMQI</font></span></i></div><div style="margin-bottom:0.0001pt;padding:0px;line-height:normal;background:white;"><i id="ydpcf278505yui_3_16_0_1_1497025264182_38323"><span id="ydpcf278505yui_3_16_0_1_1497025264182_38322" style="line-height:17.12px;color:rgb(0, 0, 191);font-size:medium;"><font face="Courier New, courier, monaco, monospace, sans-serif" id="ydpcf278505yui_3_16_0_1_1497025264182_38695" style="background-color: inherit;">http://lattes.cnpq.br/8996149312896520</font></span></i></div></div></div></div></body></html>