<html><head></head><body><div class="ydpcd08ddb7yahoo-style-wrap" style="font-family:bookman old style, new york, times, serif;font-size:16px;"><div><div dir="ltr" data-setdir="false">Prezado Fernando, primeiro obrigado pela atenção!!!</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false">Ficou mais ou menos o que eu queria, porque os códigos foram para o topo da tabela (ou seja, como nome), mas eles deveriam ser células. Vou ver se consigo adaptar!!!</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false">Obrigadão!!!</div><div><br></div><div class="ydpcd08ddb7signature"><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="ydp51d0c389yahoo_quoted_5362810945" class="ydp51d0c389yahoo_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:46:32 BRT, Fernando Souza <nandodesouza@gmail.com> escreveu:
                </div>
                <div><br></div>
                <div><br></div>
                <div><div id="ydp51d0c389yiv6784016241"><div><div>Veja se é isso</div><br clear="none"><div>library(dplyr)</div><div>library(tidyr)</div><br clear="none"><div>x01<-c(1001,"Potássio",3.49,3.62,3.63,3.49,3.44,2)</div><div>x02<-c(2000,"Potássio",4.18,4.21,4.15,4.86,4.36,3)</div><div>x03<-c(1002,"Potássio",3.49,3.62,3.63,3.49,3.44,2)</div><div>x04<-c(1003,"Potássio",3.49,3.62,3.63,3.49,3.44,2)</div><div>x05<-c(2001,"Potássio",4.18,4.21,4.15,4.86,4.36,3)</div><div>x06<-c(2000,"Albumina",2.35,2.36,2.21,2.15,2.27,10)</div><div>x07<-c(3000,"Albumina",2.69,2.78,2.88,2.91,2.84,12)</div><div>x08<-c(2001,"Albumina",2.35,2.36,2.21,2.15,2.27,10)</div><div>x09<-c(3001,"Albumina",2.69,2.78,2.88,2.91,2.84,12)</div><br clear="none"><div>exemplo<-data.frame(rbind(x01,x02,x03,x04,x05,x06,x07,x08,x09))</div><div>names(exemplo)<-c("CodCli","Substância","v1","v2","v3","v4","v5","Cluster")</div><div>row.names(exemplo)<-NULL</div><div>exemplo<-exemplo %>%mutate(v1=as.numeric(v1),v2=as.numeric(v2),v3=as.numeric(v3),v4=as.numeric(v4),v5=as.numeric(v5))</div><div>exemplo$Média<-rowMeans(exemplo[,3:7],na.rm = TRUE)</div><div>exemplo</div><br clear="none"><div>exemplo%>%gather(obs,valor, -c("CodCli","Substância","Cluster","Média"))%>%</div><div>  spread(key=CodCli,value=valor)</div><br clear="none"><div id="ydp51d0c389yiv6784016241yqt58475" class="ydp51d0c389yiv6784016241yqt8768190354"><div class="ydp51d0c389yiv6784016241gmail_quote_attribution">On Feb 16 2022, at 8:46 am, Diogo Jerônimo por (R-br) <r-br@listas.c3sl.ufpr.br> wrote:</div><blockquote><div class="ydp51d0c389yiv6784016241ydp9b0706abyahoo-style-wrap"><div><font style="font-family:old times, serif;"><font 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):</font></font></div><div><br clear="none"></div><div><div><div><div><font style="font-size:16px;"><font style="font-family:old times, serif;"><span style="color:rgb(128, 128, 128);"><font style="font-size: small; background-color: inherit;">library(dplyr)</font></span></font></font></div></div><div><div><font style="font-size:16px;"><font style="font-family:old times, serif;"><span style="color:rgb(128, 128, 128);"><font style="font-size: small; background-color: inherit;">x01<-c(1001,"Potássio",3.49,3.62,3.63,3.49,3.44,2)</font></span></font></font></div></div><div><font style="font-size:16px;"><font style="font-family:old times, serif;"><span style="color:#808080;">x02<-c(2000,"Potássio",4.18,4.21,4.15,4.86,4.36,3)</span></font></font></div><div><font style="font-size:16px;"><font style="font-family:old times, serif;"><span style="color:#808080;">x03<-c(1002,"Potássio",3.49,3.62,3.63,3.49,3.44,2)</span></font></font></div><div><font style="font-size:16px;"><font style="font-family:old times, serif;"><span style="color:#808080;">x04<-c(1003,"Potássio",3.49,3.62,3.63,3.49,3.44,2)</span></font></font></div><div><font style="font-size:16px;"><font style="font-family:old times, serif;"><span style="color:#808080;">x05<-c(2001,"Potássio",4.18,4.21,4.15,4.86,4.36,3)</span></font></font></div><div><font style="font-size:16px;"><font style="font-family:old times, serif;"><span style="color:#808080;">x06<-c(2000,"Albumina",2.35,2.36,2.21,2.15,2.27,10)</span></font></font></div><div><font style="font-size:16px;"><font style="font-family:old times, serif;"><span style="color:#808080;">x07<-c(3000,"Albumina",2.69,2.78,2.88,2.91,2.84,12)</span></font></font></div><div><font style="font-size:16px;"><font style="font-family:old times, serif;"><span style="color:#808080;">x08<-c(2001,"Albumina",2.35,2.36,2.21,2.15,2.27,10)</span></font></font></div><div><font style="font-size:16px;"><font style="font-family:old times, serif;"><span style="color:#808080;">x09<-c(3001,"Albumina",2.69,2.78,2.88,2.91,2.84,12)</span></font></font></div><div><br clear="none"></div><div><font style="font-size:16px;"><font style="font-family:old times, serif;"><span style="color:#808080;">exemplo<-data.frame(rbind(x01,x02,x03,x04,x05,x06,x07,x08,x09))</span></font></font></div><div><font style="font-size:16px;"><font style="font-family:old times, serif;"><span style="color:#808080;">names(exemplo)<-c("CodCli","Substância","v1","v2","v3","v4","v5","Cluster")</span></font></font></div><div><font style="font-size:16px;"><font style="font-family:old times, serif;"><span style="color:#808080;">row.names(exemplo)<-NULL</span></font></font></div><div><br clear="none"></div><div><div><div><div><div><font style="font-size:16px;"><font style="font-family:old times, serif;"><span style="color:#808080;">exemplo<-exemplo %>%</span></font></font></div><div><font style="font-size:16px;"><font style="font-family:old times, serif;"><span style="color:#808080;">mutate(v1=as.numeric(v1),v2=as.numeric(v2),v3=as.numeric(v3),v4=as.numeric(v4),</span></font></font></div><div><font style="font-size:16px;"><font style="font-family:old times, serif;"><span style="color:#808080;">       v5=as.numeric(v5))</span></font></font></div></div></div><div><br clear="none"></div><div><div><font style="font-size:16px;"><font style="font-family:old times, serif;"><span style="color:rgb(128, 128, 128);"><font style="font-size: small; background-color: inherit;">exemplo$Média<-rowMeans(exemplo[,3:7],na.rm = TRUE)</font></span></font></font></div></div><div><br clear="none"></div></div></div><div><font style="font-family:old times, serif;"><font 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.</font></font></div><div><br clear="none"></div><div><font style="font-family:old times, serif;"><font 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:</font></font></div><div><br clear="none"></div><div><strong><font style="font-size:16px;"><font style="font-family:old times, serif;">Subst       Cluster    Média       CodCli.1   CodCli.2   CodCli.3</font></font></strong></div><div><div><div><font style="font-family:old times, serif;"><font style="font-size:16px;">Albumina 10 2,2680 2000 2001</font></font></div><div><font style="font-family:old times, serif;"><font style="font-size:16px;">Albumina 12 2,8200    3000 3001</font></font></div><div><font style="font-family:old times, serif;"><font style="font-size:16px;">Potássio 2 3,5340 1001 1002 1003</font></font></div><div><font style="font-family:old times, serif;"><font style="font-size:16px;">Potássio 3 4,3520 2000 2001</font></font></div></div><br clear="none"></div><div><div><div><div><font style="font-family:old times, serif;"><font style="font-size:16px;">Eu tentei usar o comando </font></font><font style="font-family:old times, serif;"><font style="font-size:16px;"><span style="color:#cd232c;">reshape</span></font></font><font style="font-family:old times, serif;"><font style="font-size:16px;">, com esse código abaixo, mas ele não funcionou comigo (tentei também o </font></font><font style="font-family:old times, serif;"><font style="font-size:16px;"><span style="color:#cd232c;">spread</span></font></font><font style="font-family:old times, serif;"><font style="font-size:16px;">, do tidyr): </font></font></div></div></div><br clear="none"></div><div><div><font style="font-size:16px;"><font style="font-family:old times, serif;"><span style="color:#808080;">exemplo<-exemplo %>%</span></font></font></div></div><div><font style="font-size:16px;"><font style="font-family:old times, serif;"><span style="color:#808080;">  select(CodCli,Substância,Cluster,Média)</span></font></font></div><div><br clear="none"></div><div><div><font style="font-size:16px;"><font style="font-family:old times, serif;"><span style="color:#808080;">exemplo_quebra<-reshape(exemplo,</span></font></font></div></div><div><font style="font-size:16px;"><font style="font-family:old times, serif;"><span style="color:#808080;">                                        direction="wide",</span></font></font></div><div><font style="font-size:16px;"><font style="font-family:old times, serif;"><span style="color:#808080;">                                        idvar=c("Substância","Cluster","Média"),</span></font></font></div><div><font style="font-size:16px;"><font style="font-family:old times, serif;"><span style="color:#808080;">                                        timevar="Cod.Cli")</span></font></font></div><div><br clear="none"></div><div><div><font style="font-family:old times, serif;"><font style="font-size:16px;">Algum amigo poderia indicar onde estou errando? Ou se teria uma rotina melhor para realizar esse procedimento?</font></font></div></div><div><br clear="none"></div><div><font style="font-family:old times, serif;"><font style="font-size:16px;">Novamente obrigado!!!</font></font></div><div><br clear="none"></div></div></div><div><div><font style="font-family:old times, serif;"><font style="font-size:16px;"><em><span style="color:rgb(0, 0, 191);"><font style="font-size: medium; background-color: inherit;"><font style="font-family:Courier New, courier, monaco, monospace, sans-serif;">Diogo Jerônimo</font></font></span></em></font></font></div></div><div class="ydp51d0c389yiv6784016241ydp9b0706absignature"><div><div><font style="font-family:old times, serif;"><font style="font-size:16px;"><font style="font-family:Helvetica, Arial, sans-serif;"><em><span style="color:rgb(0, 0, 191);"><font style="font-size: medium; background-color: inherit;"><font style="font-family:Courier New, courier, monaco, monospace, sans-serif;">Bacharel em Ciências Estatísticas - ENCE/IBGE</font></font></span></em></font></font></font></div><div><font style="font-family:old times, serif;"><font style="font-size:16px;"><font style="font-family:Helvetica, Arial, sans-serif;"><em><span style="color:rgb(0, 0, 191);"><font style="font-size: medium; background-color: inherit;"><font style="font-family:Courier New, courier, monaco, monospace, sans-serif;">Mestre em Metrologia - PUC-Rio/PósMQI</font></font></span></em></font></font></font></div><div><font style="font-family:old times, serif;"><font style="font-size:16px;"><font style="font-family:Helvetica, Arial, sans-serif;"><em><span style="color:rgb(0, 0, 191);"><font style="font-size: medium; background-color: inherit;"><font style="font-family:Courier New, courier, monaco, monospace, sans-serif;">http://lattes.cnpq.br/8996149312896520</font></font></span></em></font></font></font></div></div></div></div><div>_______________________________________________</div><div>R-br mailing list</div><div>R-br@listas.c3sl.ufpr.br</div><div>https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br</div><div>Leia o guia de postagem (http://www.leg.ufpr.br/r-br-guia) e forneça código mínimo reproduzível.</div></blockquote></div></div></div></div>
            </div>
        </div></body></html>