[R-br] Combinar colunas de um banco de dados...

Benilton Carvalho beniltoncarvalho em gmail.com
Terša Julho 31 17:09:16 BRT 2012


e se quiser economizar uns decimos de segundo:

colSums(aperm(MultMat, c(2, 1, 3)))

2012/7/31 Fabio Mathias Corrŕa <fabio.ufla em yahoo.com.br>:
> Isso mesmo!
>
> Veja o help!
>
> ?array
> ?apply
>
>         Fßbio Mathias Corrŕa
>
>    Universidade Estadual de Santa Cruz
> Departamento de Ciŕncias Exatas e da Terra - DCET
>
>
> Campus Soane NazarÚ de Andrade, km 16 Rodovia IlhÚus-Itabuna
> CEP 45662-900. IlhÚus-Bahia
>
>
> Tel.: 73-3680-5076
> ________________________________
> De: andrebvs <andrebvs em bol.com.br>
>
> Para: r-br em listas.c3sl.ufpr.br
> Enviadas: Terša-feira, 31 de Julho de 2012 13:22
> Assunto: Re: [R-br] Combinar colunas de um banco de dados...
>
> obg Luciano!
>
>
> ________________________________
> Em 30/07/2012 09:24, Luciano F. Sgarbi < luciano.f.sgarbi em gmail.com >
> escreveu:
> #Seria isso:
> data<-read.table('clipboard',h=T)##esse ├ę aquela matriz de 12 colunas
> combinations<-combn(colnames(data),6)
> loop<-ncol(combinations)
> resu<-matrix(0,10,loop)
> colnames(resu)<-paste(rep("comb",loop),"_",1:loop,sep="")
> rownames(resu)<-paste(rep("row",10),"_",1:10,sep="")
> for(i in 1:loop){
> resu[,i]<-rowSums(data[,combinations[,i]])
> }
> resu
>
>
> 2012/7/29 andrebvs <andrebvs em bol.com.br>
>
> Obg colegas!
>
> Agora, tem como somar as linhas de cada combina├ž├úo? De forma que eu tenha
> um resultado geral do tipo:
>
> C1┬  C2┬  C3┬  ... C924
> [1,]┬ ┬ ┬  5┬ ┬  5
> [1,] ┬ ┬  6┬ ┬  5
> [1,] ┬ ┬  4┬ ┬  6
> [1,] ┬ ┬  6┬ ┬  5
> [1,]┬ ┬ ┬  6┬ ┬  4
> [1,] ┬ ┬  4┬ ┬  7
> [1,]┬ ┬ ┬  4┬ ┬  7
> [1,] ┬ ┬  3┬ ┬  8
> [1,] ┬ ┬  9┬ ┬  8
> [1,] ┬ ┬  2 ┬  6
> .
> .
> .
> [1265,]
>
>
> thanks!
>
>
> ________________________________
> Em 29/07/2012 20:01, Luciano F. Sgarbi < luciano.f.sgarbi em gmail.com >
> escreveu:
> É isso mesmo!
>
> 2012/7/29 Pedro Rafael <pedro.rafael.marinho em gmail.com>
>
> Se quiser uma outra combina├ž├úo colocar
> dados[as.vector(colunas_combinadas[,2])]
> [ ┬  ],
> Pedro Rafael Diniz Marinho.
>
>
> Em 29 de julho de 2012 19:55, Pedro Rafael
> <pedro.rafael.marinho em gmail.com> escreveu:
>> dados = read.table("dados_teste.txt",header=TRUE)
>> nomes_das_colunas = as.vector(colnames(dados))
>> colunas_combinadas = combn(nomes_das_colunas,6)
>> dados[as.vector(colunas_combinadas[,1])]
>> [ ┬  ],
>> Pedro Rafael Diniz Marinho.
>>
>>
>> Em 29 de julho de 2012 19:47, andrebvs [via R-br]
>> <ml-node+s2285057n4655711h18 em n4.nabble.com> escreveu:
>>> Olá Luciano!
>>>
>>> A d├║vida ├ę a seguinte:
>>> 1┬║) Tenho o seguinte banco de dados abaixo:
>>>
>>>
>>> Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 Q11 Q12
>>> 1 ┬  ┬  1 ┬ 0 ┬  3 ┬  1 ┬  0 ┬  0 ┬  0 ┬  1 ┬  0 ┬  ┬ 1 ┬  ┬ 0 ┬  ┬ 1
>>> 2 ┬  ┬  1 ┬ 3 ┬  0 ┬  0 ┬  1 ┬  1 ┬  0 ┬  1 ┬  1 ┬  ┬ 0 ┬  ┬ 2 ┬  ┬ 1
>>> 3 ┬  ┬  0 ┬ 2 ┬  1 ┬  0 ┬  0 ┬  1 ┬  3 ┬  0 ┬  0 ┬   ┬ 1 ┬  ┬ 0 ┬  ┬ 0
>>> 4 ┬  ┬  1 ┬ 0 ┬  0 ┬  2 ┬  2 ┬  1 ┬  0 ┬  0 ┬  0 ┬  ┬ 0 ┬  ┬ 2 ┬  ┬ 1
>>> 5 ┬  ┬  0 ┬ 2 ┬  1 ┬  0 ┬  1 ┬  2 ┬  0 ┬  1 ┬  1 ┬  ┬ 1 ┬  ┬ 0 ┬  ┬ 2
>>> 6 ┬  ┬  1 ┬ 1 ┬  0 ┬  1 ┬  1 ┬  0 ┬  3 ┬  1 ┬  2 ┬  ┬ 1 ┬  ┬ 1 ┬  ┬ 0
>>> 7 ┬  ┬  1 ┬ 1 ┬  0 ┬  1 ┬  1 ┬  0 ┬  0 ┬  0 ┬  0 ┬  ┬ 1 ┬  ┬ 1 ┬  ┬ 1
>>> 8 ┬  ┬  0 ┬ 1 ┬  0 ┬  1 ┬  0 ┬  1 ┬  2 ┬  0 ┬  2 ┬  ┬ 0 ┬  ┬ 3 ┬  ┬ 0
>>> 9 ┬  ┬  1 ┬ 2 ┬  3 ┬  1 ┬  2 ┬  0 ┬  0 ┬  0 ┬  0 ┬  ┬ 2 ┬  ┬ 0 ┬  ┬ 1
>>> 10 ┬  ┬ 0 ┬ 1 ┬  0 ┬ 1 ┬  0 ┬  0 ┬  1 ┬  0 ┬  1 ┬  ┬ 1 ┬  ┬ 0 ┬  ┬ 2
>>>
>>> 2┬║) Quero combinar todas as 12 colunas tomadas 6 ├  6, fazendo as contas
>>> terei 924 combina├ž├Áes possiv├ęis, e uma combina├ž├úo poss├şvel seria,
>>> por
>>> exemplo a seguinte:
>>>
>>> Q1 ┬ Q4 ┬ Q7 Q8 Q9 ┬ Q12
>>> 1 ┬  ┬  1 ┬  ┬ 1 ┬  ┬ 0 ┬  1 ┬  0 ┬  ┬ 1
>>> 2     1    0    0 Â   1   1    1
>
>>> 3 ┬  ┬  0 ┬  ┬ 0 ┬  ┬ 3 ┬  0 ┬  0 ┬  ┬ 0
>>> 4 ┬  ┬  1 ┬  ┬ 2 ┬  ┬ 0 ┬  0 ┬  0 ┬  ┬ 1
>>> 5 ┬  ┬  0 ┬  ┬ 0 ┬  ┬ 0 ┬  1 ┬  1 ┬  ┬ 2
>>> 6 ┬  ┬  1 ┬  ┬ 1 ┬  ┬ 3 ┬  1 ┬  2 ┬  ┬ 0
>>> 7 ┬  ┬  1 ┬  ┬ 1 ┬  ┬ 0 ┬  0 ┬  0 ┬  ┬ 1
>>> 8 ┬  ┬  0 ┬  ┬ 1 ┬  ┬ 2 ┬  0 ┬  2 ┬  ┬ 0
>>> 9 ┬  ┬  1 ┬  ┬ 1 ┬  ┬ 0 ┬  0 ┬  0 ┬  ┬ 1
>>> 10 ┬  ┬ 0 ┬  1 ┬  ┬ 1 ┬  0 ┬  1 ┬  ┬  2
>>>
>>> Repare que, das 12 colunas iniciais, tenho essa ├║ltima com 6 colunas, a
>>> qual
>>> ├ę uma poss├şvel combina├ž├úo das 924. Ent├úo, como extrair todas as 924
>>> combina├ž├Áes da matriz inicial?
>>>
>>> 3┬║) Ap├│s isso, ou seja, ap├│s ter encontrado todas as 924
>>> combina├ž├Áes,
>>> gostaria tamb├ęm, que tivesse um vetor q ue me retorne a soma de cada
>>> linha
>>> de cada combina├ž├úo. Por exemplo:
>>>
>>> combin a├ž├úo1
>>> combina├ž├úo924
>>> Q1 ┬ Q4 ┬ Q7 Q8 Q9 ┬ Q12 ┬  ┬ soma das linhas ┬  ┬  ┬  ┬  ┬ . . . Q6 ┬ Q1
>>> ┬ Q11 ┬ Q2 Q9
>>> Q7 ┬  ┬  ┬  ┬  ┬ soma das linhas
>>> 1 ┬  ┬  1 ┬  ┬ 1 ┬  ┬ 0 ┬  1 ┬  0 ┬  ┬ 1 ┬  ┬  ┬  ┬  ┬  ┬ 4
>>> 0 ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬
>>> 2
>>> 2 ┬  ┬  1 ┬  ┬ 0 ┬  ┬ 0 ┬  1 ┬  1 ┬  ┬ 1 ┬  ┬  ┬  ┬  ┬  ┬ 4
>>> 1 ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬
>>> 3
>>> 3 ┬  ┬  0 ┬  ┬ 0 ┬  ┬ 3 ┬  0 ┬  0 ┬  ┬ 0 ┬  ┬  ┬  ┬  ┬  ┬ 3 ┬  ┬  ┬  ┬  ┬
>>> ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬ ´┐Ż ´┐Ż
>>> 3 ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬
>>> 5
>>> 4 ┬  ┬  1 ┬  ┬ 2 ┬  ┬ 0 ┬  0 ┬  0 ┬  ┬ 1 ┬  ┬  ┬  ┬  ┬  ┬ 4
>>> 3 ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬
>>> 1
>>> 5 ┬  ┬  0 ┬  ┬ 0 ┬  ┬ 0 ┬  1 ┬  1 ┬  ┬ 2 ┬  ┬  ┬  ┬  ┬  ┬ 4
> &g t;> . ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬
> ┬  ┬ .
>
>>> 6 ┬  ┬  1 ┬  ┬ 1 ┬  ┬ 3 ┬  1 ┬  2 ┬  ┬ 0 ┬  ┬  ┬  ┬  ┬  ┬ 8
>>> . ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬
>>> ┬ .
>>> 7 ┬  ┬  1 ┬  ┬ 1 ┬  ┬ 0 ┬  0 ┬  0 ┬  ┬ 1 ┬  ┬  ┬  ┬  ┬  ┬ 3 ┬  ┬  ┬  ┬  ┬
>>> ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ´┐Ż
>>> ´┐Ż ┬  ┬  . ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬
>>> ┬  ┬  ┬  ┬ .
>>> 8 ┬  ┬  0 ┬  ┬ 1 ┬  ┬ 2 ┬  0 ┬  2 ┬  ┬ 0 ┬  ┬  ┬  ┬  ┬  ┬ 5
>>> etc ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬  ┬
>>> etc
>>> 9 ┬  ┬  1 ┬  ┬ 1 ┬  ┬ 0 ┬  0 ┬  0 ┬  ┬ 1 ┬  ┬  ┬  ┬  ┬  ┬ 3
>>> 10 ┬  ┬ 0 ┬  1 ┬  ┬ 1 ┬  0 ┬  1 ┬  ┬  2 ┬  ┬  ┬  ┬  ┬  ┬ 5
>>>
>>> desde j├í agrade├žo!
>>>
>>>
>>> ________________________________
>>> Em 29/07/2012 19:04, Luciano F. Sgarbi < [hidde n email] > escreveu:
>
>>> Boa tarde.
>>> Tentei, mas não entendi sua pergunta.
>>> Você pode ser um pouco mais especifico?
>>> Abra├žos
>>>
>>> 2012/7/29 andrebvs
>>>>
>>>> Ol├í pessoal, gostaria de saber como fa├žo para combinar essas 12
>>>> colunas
>>>> (Q1 at├ę Q12) tomados 6 ├  6 colunas:
>>>>
>>>> Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 Q11 Q12
>>>> 1 ┬  ┬  1 ┬ 0 ┬  3 ┬  1 ┬  0 ┬  0 ┬  0 ┬  1 ┬  0 ┬  ┬ 1 ┬  ┬ 0 ┬  ┬ 1
>>>> 2 ┬  ┬  1 ┬ 3 ┬  0 ┬  0 ┬  1 ┬  1 ┬  0 ┬  1 ┬  1 ┬  ┬ 0 ┬  ┬ 2 ┬  ┬ 1
>>>> 3 ┬  ┬  0 ┬ 2 ┬  1 ┬  0 ┬  0 ┬  1 ┬  3 ┬  0 ┬  0 ┬  ┬ 1 ┬  ┬ 0 ┬  ┬ 0
>>>> 4 ┬  ┬  1 ┬ 0 ┬  0 ┬  2 ┬  2 ┬  1 ┬  0 ┬  0 ┬  0 ┬  ┬ 0 ┬  ┬ 2 ┬  ┬ 1
>>>> 5 ┬  ┬  0 ┬ 2 ┬  1 ┬  0 ┬  1 ┬  2 ┬  0 ┬  1 ┬  1 ┬  ┬ 1 ┬  ┬ 0 ┬  ┬ 2
>>>> 6     1  1   0   1   1   0   3   1 Â   2    1    1    0
>
>>>> 7 ┬  ┬  1 ┬ 1 ┬  0 ┬  1 ┬  1 ┬  0 ┬  0 ┬  0 ┬  0 ┬  ┬ 1 ┬  ┬ 1 ┬  ┬ 1
>>>> 8 ┬  ┬  0 ┬ 1 ┬  0 ┬  1 ┬  0 ┬  1 ┬  2 ┬  0 ┬  2 ┬  ┬ 0 ┬  ┬ 3 ┬  ┬ 0
>>>> 9 ┬  ┬  1 ┬ 2 ┬  3 ┬  1 ┬  2 ┬  0 ┬  0 ┬  0 ┬  0 ┬  ┬ 2 ┬  ┬ 0 ┬  ┬ 1
>>>> 10 ┬  ┬ 0 ┬ 1 ´┐Ż ´┐Ż 0 ┬ 1 ┬  0 ┬  0 ┬  1 ┬  0 ┬  1 ┬  ┬ 1 ┬  ┬ 0 ┬  ┬
>>>> 2
>>>>
>>>> desde j├í agrade├žo!
>>>>
>>>> Att.
>>>> Andr├ę
>>>>
>>>>
>>>> _______________________________________________
>>>> R-br mailing list
>>>> R-br em ...
>>>>
>>>> https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br
>>>> Leia o guia de postagem (http://www.leg.ufpr.b r/r-br-guia) e forne├ža
>
>>>> c├│digo m├şnimo reproduz├şvel.
>>>
>>>
>>>
>>> --
>>> Luciano F. Sgarbi
>>> Mestrando em Ecologia e Evolu├ž├úo - UFG
>>>
>>> Laborat├│rio de Ecologia de Insetos
>>> Cel. (62)8174-2262 Lab. (62)3521-1732
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> _______________________________________________
>>> R-br mailing list
>>> [hidden email]
>>> https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br
>>> Leia o guia de postagem (http://www.leg.ufpr.br/r-br-guia) e forne├ž a
>>> c├│digo
>
>>> m├şnimo reproduz├şvel.
>>>
>>> ________________________________
>>> If you reply to this email, your message will be added to the discussion
>>> below:
>>>
>>> http://r-br.2285057.n4.nabble.com/R-br-Combinar-colunas-de-um-banco-de-dados-tp4655709p4655711.html
>>> To unsubscribe from R-br, click here.
>>> NAML
> _______________________________________________
> R-br mailing list
> R-br em listas.c3sl.ufpr.br
> https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br
> Leia o guia de postagem (http://www.leg.ufpr .br/r-br-guia) e forne├ža
> c├│digo m├şnimo reproduz├şvel.
>
>
>
> --
> Luciano F. Sgarbi
> Mestrando┬ em Ecologia e Evolu├ž├úo - UFG
> Laborat├│rio de Ecologia de Insetos
> Cel. (62)8174-2262 Lab. (62)3521-1732
>>>
>
>
> _______________________________________________
> R-br mailing list
> R-br em listas.c3sl.ufpr.br
> https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br
> Leia o guia de postagem (http://www.leg.ufpr.br/r-br-guia) e forne├ža
> c├│digo m├şnimo reproduz├şvel.
>
>
>
> --
> Luciano F. Sgarbi
> Mestrando┬ em Ecologia e Evolu├ž├úo - UFG
>
> Laborat├│rio de Ecologia de Insetos
> Cel. (62)8174-2262 ┬  Lab. (62)3521-1732
>>>
>
> _______________________________________________
> R-br mailing list
> R-br em listas.c3sl.ufpr.br
> https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br
> Leia o guia de postagem (http://www.leg.ufpr.br/r-br-guia) e forneša cˇdigo
> mÝnimo reproduzÝvel.
>
>
> _______________________________________________
> R-br mailing list
> R-br em listas.c3sl.ufpr.br
> https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br
> Leia o guia de postagem (http://www.leg.ufpr.br/r-br-guia) e forneša cˇdigo
> mÝnimo reproduzÝvel.


Mais detalhes sobre a lista de discussão R-br