<html><head></head><body><div class="ydpcf17e225yahoo-style-wrap" style="font-family:bookman old style, new york, times, serif;font-size:16px;"><div><div dir="ltr" data-setdir="false"><div><div dir="ltr" data-setdir="false">Prezado Cid, primeiro obrigado pela atenção!!!</div></div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false">Era exatamente o que eu queria, só não ficou perfeito porque você quadrou com meu exemplo exato (com três códigos), mas podem ter mais de três (não saberia dizer quantos), então terei de ver como ampliar sua solução. Mas era exatamente isso!!!</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false">Muitíssimo obrigado!!! </div></div><div><br></div><div class="ydpcf17e225signature"><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_38508"><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></div><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>
        <div><br></div><div><br></div>
        
        </div><div id="ydp6f1e5decyahoo_quoted_5356251011" class="ydp6f1e5decyahoo_quoted">
            <div style="font-family:'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:13px;color:#26282a;">
                
                <div>
                    Em quarta-feira, 16 de fevereiro de 2022 09:35:30 BRT, Cid Póvoas <cidedson@gmail.com> escreveu:
                </div>
                <div><br></div>
                <div><br></div>
                <div><div id="ydp6f1e5decyiv0367194914"><div><div dir="ltr">library(dplyr)<br clear="none">x01<-c(1001,"Potássio",3.49,3.62,3.63,3.49,3.44,2)<br clear="none">x02<-c(2000,"Potássio",4.18,4.21,4.15,4.86,4.36,3)<br clear="none">x03<-c(1002,"Potássio",3.49,3.62,3.63,3.49,3.44,2)<br clear="none">x04<-c(1003,"Potássio",3.49,3.62,3.63,3.49,3.44,2)<br clear="none">x05<-c(2001,"Potássio",4.18,4.21,4.15,4.86,4.36,3)<br clear="none">x06<-c(2000,"Albumina",2.35,2.36,2.21,2.15,2.27,10)<br clear="none">x07<-c(3000,"Albumina",2.69,2.78,2.88,2.91,2.84,12)<br clear="none">x08<-c(2001,"Albumina",2.35,2.36,2.21,2.15,2.27,10)<br clear="none">x09<-c(3001,"Albumina",2.69,2.78,2.88,2.91,2.84,12)<br clear="none"><br clear="none">exemplo<-data.frame(rbind(x01,x02,x03,x04,x05,x06,x07,x08,x09))<br clear="none">names(exemplo)<-c("CodCli","Substância","v1","v2","v3","v4","v5","Cluster")<br clear="none">row.names(exemplo)<-NULL<br clear="none"><br clear="none">exemplo<-exemplo %>%<br clear="none">  mutate(v1=as.numeric(v1),v2=as.numeric(v2),v3=as.numeric(v3),v4=as.numeric(v4),<br clear="none">         v5=as.numeric(v5))<br clear="none"><br clear="none">exemplo$Média<-rowMeans(exemplo[,3:7],na.rm = TRUE)<br clear="none"><br clear="none"><font color="#ff0000">exemplo %>% pivot_longer(cols = 3:7) %>% <br clear="none">  group_by(Cluster, Substância, Média) %>% <br clear="none">  select(-value) %>% <br clear="none">  pivot_wider(names_from = name, values_from = CodCli) %>% <br clear="none">  as.data.frame() %>% pivot_longer(cols=-c(1:3)) %>% as.data.frame() %>% select(-name) %>% <br clear="none">  separate(value, c("x","CodCli.1","CodCli.2","CodCli.3")) %>% select(-x) %>% <br clear="none">  unique() %>% as.data.frame() %>% arrange(Substância)</font><br clear="all"><div><div dir="ltr" class="ydp6f1e5decyiv0367194914gmail_signature"><div dir="ltr"><div style="text-align:left;"><br clear="none"></div><div style="text-align:left;"><br clear="none"></div><div style="text-align:left;"><br clear="none"></div><div style="text-align:left;">Vê se isso ajuda...</div><div style="text-align:left;"><br clear="none"></div><div style="text-align:center;"><div style="text-align:left;"><font color="#073763"><b><i>Cid Edson Mendonça Póvoas</i></b></font></div><div style="text-align:left;"><font color="#073763"><b><i><br clear="none"></i></b></font></div><div style="text-align:left;"><b style="font-size:12.8px;color:rgb(7,55,99);"><i><a shape="rect" href="http://www.anovagro.com/" rel="nofollow" target="_blank">AnovAgro</a></i></b><br clear="none"></div><div style="text-align:left;"><b><i><font color="#073763">Engenheiro Agrônomo - </font></i></b><i style="color:rgb(7,55,99);"><b>Data Scientist</b></i><b><i><font color="#073763"> </font></i></b></div><div><div style="text-align:left;"><i style="color:rgb(7,55,99);"><b>CREA :</b> 051984991-4</i><b><font color="#073763"><i><br clear="none"></i></font></b></div><div style="text-align:left;"><b><font color="#073763"><i>Técnico em Segurança do Trabalho </i></font></b><br clear="none"></div><div style="text-align:left;"><i style="color:rgb(7,55,99);"><b>Nº: </b></i><span style="text-align:center;"><font color="#073763" style="background-color: inherit;"><i>0012669/BA</i></font></span></div><div style="text-align:left;"><i style="color:rgb(7,55,99);"><b>Tel: </b>+55 73 99151-9565</i></div><div style="text-align:left;"><font color="#073763"><span style="font-size:12.8px;"><b>Lattes : </b></span><span style="font-size:12.8px;"><a shape="rect" href="http://lattes.cnpq.br/2303498368142537" rel="nofollow" target="_blank">http://lattes.cnpq.br/2303498368142537</a></span><br clear="none"></font></div></div><div><div><div style="text-align:left;"><span style="font-size:12.8px;"><font color="#073763" style="background-color: inherit;"><b>LinkedIn :</b> <a shape="rect" href="http://br.linkedin.com/in/cidedson/" rel="nofollow" target="_blank">http://br.linkedin.com/in/cidedson/</a></font></span></div><div style="text-align:left;"><font color="#073763"><b>Whatsapp :</b> <a shape="rect" href="https://wa.me/5573991519565" rel="nofollow" target="_blank">https://wa.me/5573991519565</a></font></div></div></div></div></div></div></div><br clear="none"></div><br clear="none"><div class="ydp6f1e5decyiv0367194914gmail_quote"><div dir="ltr" class="ydp6f1e5decyiv0367194914gmail_attr">Em qua., 16 de fev. de 2022 às 08:47, Diogo Jerônimo por (R-br) <<a shape="rect" href="mailto:r-br@listas.c3sl.ufpr.br" rel="nofollow" target="_blank">r-br@listas.c3sl.ufpr.br</a>> escreveu:<br clear="none"></div><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex;" class="ydp6f1e5decyiv0367194914gmail_quote"><div id="ydp6f1e5decyiv0367194914yqt09221" class="ydp6f1e5decyiv0367194914yqt7590926275"><div><div style="font-family:old times, serif;font-size:16px;"><div dir="ltr" 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" style="font-size:16px;"><br clear="none"></div><div dir="ltr"><div><div><span style="color:rgb(128,128,128);font-size:small;">library(dplyr)</span><br clear="none"></div><div><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 clear="none"></div><div><font color="#808080" size="2">x02<-c(2000,"Potássio",4.18,4.21,4.15,4.86,4.36,3)</font></div><div><font color="#808080" size="2">x03<-c(1002,"Potássio",3.49,3.62,3.63,3.49,3.44,2)</font></div><div><font color="#808080" size="2">x04<-c(1003,"Potássio",3.49,3.62,3.63,3.49,3.44,2)</font></div><div><font color="#808080" size="2">x05<-c(2001,"Potássio",4.18,4.21,4.15,4.86,4.36,3)</font></div><div><font color="#808080" size="2">x06<-c(2000,"Albumina",2.35,2.36,2.21,2.15,2.27,10)</font></div><div><font color="#808080" size="2">x07<-c(3000,"Albumina",2.69,2.78,2.88,2.91,2.84,12)</font></div><div><font color="#808080" size="2">x08<-c(2001,"Albumina",2.35,2.36,2.21,2.15,2.27,10)</font></div><div><font color="#808080" size="2">x09<-c(3001,"Albumina",2.69,2.78,2.88,2.91,2.84,12)</font></div><div><font color="#808080" size="2"><br clear="none"></font></div><div><font color="#808080" size="2">exemplo<-data.frame(rbind(x01,x02,x03,x04,x05,x06,x07,x08,x09))</font></div><div><font color="#808080" size="2">names(exemplo)<-c("CodCli","Substância","v1","v2","v3","v4","v5","Cluster")</font></div><div><font color="#808080" size="2">row.names(exemplo)<-NULL</font></div><div style="font-size:16px;"><br clear="none"></div><div dir="ltr"><div><div dir="ltr"><font color="#808080" size="2"></font><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></div><div><span style="color:rgb(128,128,128);font-size:small;"><br clear="none"></span></div><div><span style="color:rgb(128,128,128);font-size:small;">exemplo$Média<-rowMeans(exemplo[,3:7],na.rm = TRUE)</span><br clear="none"></div><div style="font-size:16px;"><br clear="none" style="color:rgb(0,0,0);font-family:old times, serif;"></div></div></div><div dir="ltr" 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" style="font-size:16px;"><br clear="none"></div><div dir="ltr" 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 clear="none"></div><div dir="ltr" style="font-size:16px;"><b><span style="background-color:rgb(253,248,105);">Subst       Cluster    Média       CodCli.1   </span><span style="color:rgb(0,0,0);font-family:old times, serif;font-size:16px;"><span style="background-color:rgb(253,248,105);">CodCli.2   <span style="color:rgb(0,0,0);font-family:old times, serif;font-size:16px;">CodCli.3</span></span></span></b></div><div dir="ltr" style="font-size:16px;"><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 clear="none"></div><div dir="ltr" style="font-size:16px;"><div><div dir="ltr">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 clear="none"></div></div><br clear="none"></div><div><font color="#808080" size="2">exemplo<-exemplo %>%<br clear="none"></font></div><div><font color="#808080" size="2">  select(CodCli,Substância,Cluster,Média)</font></div><div><font color="#808080" size="2"><br clear="none"></font></div><div><font color="#808080" size="2">exemplo_quebra<-reshape(exemplo,<br clear="none"></font></div><div><font color="#808080" size="2">                                        direction="wide",</font></div><div><font color="#808080" size="2">                                        idvar=c("Substância","Cluster","Média"),</font></div><div><font color="#808080" size="2">                                        timevar="Cod.Cli")</font></div><div style="font-size:16px;"><br clear="none"></div><div dir="ltr" style="font-size:16px;">Algum amigo poderia indicar onde estou errando? Ou se teria uma rotina melhor para realizar esse procedimento?<br clear="none"></div><div dir="ltr" style="font-size:16px;"><br clear="none"></div><div dir="ltr" style="font-size:16px;">Novamente obrigado!!!</div><div style="font-size:16px;"><br clear="none"></div></div></div><div style="font-size:16px;"><i id="ydp6f1e5decyiv0367194914gmail-m_-2974581309438902227ydpcf278505yui_3_16_0_1_1497025264182_38508" style="font-family:Helvetica, Arial, sans-serif;"><span id="ydp6f1e5decyiv0367194914gmail-m_-2974581309438902227ydpcf278505yui_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 clear="none"></div><div 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="ydp6f1e5decyiv0367194914gmail-m_-2974581309438902227ydpcf278505yui_3_16_0_1_1497025264182_38430"><span id="ydp6f1e5decyiv0367194914gmail-m_-2974581309438902227ydpcf278505yui_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="ydp6f1e5decyiv0367194914gmail-m_-2974581309438902227ydpcf278505yui_3_16_0_1_1497025264182_38309"><span id="ydp6f1e5decyiv0367194914gmail-m_-2974581309438902227ydpcf278505yui_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="ydp6f1e5decyiv0367194914gmail-m_-2974581309438902227ydpcf278505yui_3_16_0_1_1497025264182_38323"><span id="ydp6f1e5decyiv0367194914gmail-m_-2974581309438902227ydpcf278505yui_3_16_0_1_1497025264182_38322" style="line-height:17.12px;color:rgb(0,0,191);font-size:medium;"><font id="ydp6f1e5decyiv0367194914gmail-m_-2974581309438902227ydpcf278505yui_3_16_0_1_1497025264182_38695" face="Courier New, courier, monaco, monospace, sans-serif" style="background-color:inherit;"><a shape="rect" href="http://lattes.cnpq.br/8996149312896520" rel="nofollow" target="_blank">http://lattes.cnpq.br/8996149312896520</a></font></span></i></div></div></div></div></div></div>_______________________________________________<br clear="none">
R-br mailing list<br clear="none">
<a shape="rect" href="mailto:R-br@listas.c3sl.ufpr.br" rel="nofollow" target="_blank">R-br@listas.c3sl.ufpr.br</a><br clear="none">
<a shape="rect" href="https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br" rel="nofollow" target="_blank">https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br</a><br clear="none">
Leia o guia de postagem (<a shape="rect" href="http://www.leg.ufpr.br/r-br-guia" rel="nofollow" target="_blank">http://www.leg.ufpr.br/r-br-guia</a>) e forneça código mínimo reproduzível.<br clear="none">
</blockquote></div>
</div></div></div>
            </div>
        </div></body></html>