[R-br] SOMAR LINHAS
Rodrigo Coster
rcoster em gmail.com
Quinta Maio 4 16:41:25 BRT 2017
Jose,
Segue uma solução usando o aggregate() (mas já digo que existem funções
melhores para isso). Os teus dados estavam um pouco bagunçados, com numeros
sendo lidos como texto, por isso mexi (apenas corrigi isso) um pouco neles.
dados <- structure(list(id = c(49L, 49L, 49L, 64L, 64L, 64L, 23L, 23L,
23L, 23L), evento1 = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L,
3L, 3L, 3L), .Label = c("A", "D", "E"), class = "factor"), nomes =
structure(c(3L,
3L, 3L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("Anicuns", "Goiânia",
"Santo Antônio do Descoberto"), class = "factor"), cons_jan_15 = c(9033.36,
NA, NA, NA, NA, 4091.54, 6833.28, NA, NA, NA), valor_jan_15 = c(4848.67,
NA, NA, NA, NA, 3076.74, 3772.8, NA, NA, NA), cons_fev_15 = c(13131.13,
NA, NA, NA, NA, 5482.15, 8706.81, NA, NA, NA), valor_fev_15 = c(4320.57,
NA, NA, NA, NA, 2243.94, 2961.72, NA, NA, NA), cons_mar_15 = c(8121.07,
NA, NA, NA, NA, 3380.04, 5524.23, NA, NA, NA), valor_mar_15 = c(4410.8,
NA, NA, NA, NA, 2033.13, 3285.14, NA, NA, NA), cons_abr_15 = c(8562.85,
NA, NA, NA, NA, 3003.66, 6157.68, NA, NA, NA), valor_abr_15 = c(6241.59,
NA, NA, NA, NA, 2250.64, 4381.01, NA, NA, NA), cons_mai_15 = c(8261.5,
NA, NA, NA, NA, 3601.44, 5997.78, NA, NA, NA), valor_mai_15 = c(5586.92,
NA, NA, NA, NA, 2550.23, 4178.95, NA, NA, NA)), .Names = c("id",
"evento1", "nomes", "cons_jan_15", "valor_jan_15", "cons_fev_15",
"valor_fev_15", "cons_mar_15", "valor_mar_15", "cons_abr_15",
"valor_abr_15", "cons_mai_15", "valor_mai_15"), class = "data.frame",
row.names = c(NA,
-10L))
aggregate(. ~ id + evento1 + nomes, FUN = sum, na.rm = TRUE, data = dados)
# Dados agregados
merge(dados, aggregate(. ~ id + evento1 + nomes, FUN = sum, na.rm = TRUE,
data = dados), all = TRUE) # Juntando tudo
2017-05-04 16:26 GMT-03:00 Fernando Antonio de souza via R-br <
r-br em listas.c3sl.ufpr.br>:
> César
>
> A variável BD não é um vetor, nem outra estrutura que eu conheça no R,
> Para montar a tabela com esses dados tenho de digita-los novamente.
>
> Vc copiou e colou esse comando no seu R workspace? Deu certo?
>
> No meu esse comando não criou a variável bd.
>
> bd<-"id;evento1;nomes;cons_jan_15 ;valor_jan_15;cons_fev_15;valo
> r_fev_15;....."
>
> Isso tá longe de ser um vetor.
>
> Quero poder ajudar. Até mesmo pelo desafio. Aprendo muito quando me
> esforço a ajudar alguém.
> No entanto os dados da forma como ele enviou me obriga perder tempo
> reorganizando-o e eu não disponho de tal. Por isso solicitei q ele envie de
> outra forma para q torne mais fácil a tarefa. Através do dput() eu somente
> tenho de copiar e colar a saída no meu workspace para ter acesso ao banco
> de dados. Na firma como ele enviou isso é impossível.
>
>
>
> Em 04/05/2017 15:19, "Cesar Rabak" <cesar.rabak em gmail.com> escreveu:
>
> Fernando,
> Eu copiei e colei as duas linhas relevantes do post do José e em vez de
> somente teclar 'dads' dei um summary(dads) e o resultado foi o seguinte:
> > summary(dads)
> id evento1 nomes cons_jan_15
> Min. :23.00 A:3 Anicuns :4 Min. :4092
> 1st Qu.:23.00 D:3 Goiânia :3 1st Qu.:5462
> Median :49.00 E:4 Santo Antônio do Descoberto:3 Median :6833
> Mean :43.10 Mean :6653
> 3rd Qu.:60.25 3rd Qu.:7933
> Max. :64.00 Max. :9033
> NA's :7
> valor_jan_15 cons_fev_15 valor_fev_15 cons_mar_15 valor_mar_15
> 3076.74:1 Min. : 5482 Min. :2244 Min. :3380 Min. :2033
> 3772.8 :1 1st Qu.: 7094 1st Qu.:2603 1st Qu.:4452 1st Qu.:2659
> 4848.67:1 Median : 8707 Median :2962 Median :5524 Median :3285
> NA :2 Mean : 9107 Mean :3175 Mean :5675 Mean :3243
> NA's :5 3rd Qu.:10919 3rd Qu.:3641 3rd Qu.:6823 3rd Qu.:3848
> Max. :13131 Max. :4321 Max. :8121 Max. :4411
> NA's :7 NA's :7 NA's :7 NA's :7
> cons_abr_15 valor_abr_15 cons_mai_15 valor_mai_15
> Min. :3004 Min. :2251 Min. :3601 Min. :2550
> 1st Qu.:4581 1st Qu.:3316 1st Qu.:4800 1st Qu.:3365
> Median :6158 Median :4381 Median :5998 Median :4179
> Mean :5908 Mean :4291 Mean :5954 Mean :4105
> 3rd Qu.:7360 3rd Qu.:5311 3rd Qu.:7130 3rd Qu.:4883
> Max. :8563 Max. :6242 Max. :8262 Max. :5587
> NA's :7 NA's :7 NA's :7 NA's :7
> >
>
> Nota: para ver melhor selecione a parte acima e mude o fonte para Courier.
>
> Qual foi sua dificuldade?
>
>
> 2017-05-04 13:00 GMT-03:00 Fernando Antonio de souza <
> nandodesouza em gmail.com>:
>
>> José,
>> Seu comando não é reproduzível.
>> Dê um dput(Dados) copie a saída e cole no email, para que possamos
>> refazer e lhe ajudar aqui.
>>
>> Att
>>
>> Em 3 de maio de 2017 22:36, Cesar Rabak via R-br <
>> r-br em listas.c3sl.ufpr.br> escreveu:
>>
>>> Veja a documentação para o comando agregate, especialmente a interface
>>> com uso de "fórmulas".
>>>
>>> Talvez você tenha que fazer a concatenação de cada soma.
>>>
>>> YMMV
>>>
>>> --
>>> Cesar Rabak
>>>
>>> 2017-05-03 14:03 GMT-03:00 jose gomes de souza filho Gomes via R-br <
>>> r-br em listas.c3sl.ufpr.br>:
>>>
>>>> Boa Tarde,
>>>>
>>>> Tenho a seguinte base ( uma parte, são várias linhas):
>>>>
>>>> bd<-"id;evento1;nomes;cons_jan_15 ;valor_jan_15;cons_fev_15;valo
>>>> r_fev_15;cons_mar_15;valor_mar_15 ;cons_abr_15;valor_abr_15;cons
>>>> _mai_15;valor_mai_15
>>>> 49;A;Santo Antônio do Descoberto;9033.36;4848.67;131
>>>> 31.13;4320.57;8121.07;4410.8;8562.85;6241.59;8261.5;5586.92
>>>> 49;A;Santo Antônio do Descoberto;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA
>>>> 49;A;Santo Antônio do Descoberto;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA
>>>> 64;D;Goiânia;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA
>>>> 64;D;Goiânia;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA
>>>> 64;D;Goiânia;4091.54;3076.74;5482.15;2243.94;3380.04;2033.13
>>>> ;3003.66;2250.64;3601.44;2550.23
>>>> 23;E;Anicuns;6833.28;3772.8;8706.81;2961.72;5524.23;3285.14;
>>>> 6157.68;4381.01;5997.78;4178.95
>>>> 23;E;Anicuns;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA
>>>> 23;E;Anicuns;NA;NA ;NA;NA;NA;NA;NA;NA;NA;NA
>>>> 23;E;Anicuns;NA;NA ;NA;NA;NA;NA;NA;NA;NA;NA"
>>>> dads <- read.table(header = TRUE, text=bd, sep=";")
>>>> dads
>>>>
>>>> Quero somar as linhas, de modo que:
>>>>
>>>> id
>>>>
>>>> evento1
>>>>
>>>> nomes
>>>>
>>>> cons_jan_15
>>>>
>>>> valor_jan_15
>>>>
>>>> cons_fev_15
>>>>
>>>> valor_fev_15
>>>>
>>>> cons_mar_15
>>>>
>>>> valor_mar_15
>>>>
>>>> cons_abr_15
>>>>
>>>> valor_abr_15
>>>>
>>>> cons_mai_15
>>>>
>>>> valor_mai_15
>>>>
>>>> 49
>>>>
>>>> A
>>>>
>>>> Santo Antônio do Descoberto
>>>>
>>>> 9033,36
>>>>
>>>> 4848,67
>>>>
>>>> 13131,13
>>>>
>>>> 4320,57
>>>>
>>>> 8121,07
>>>>
>>>> 4410,8
>>>>
>>>> 8562,85
>>>>
>>>> 6241,59
>>>>
>>>> 8261,5
>>>>
>>>> 5586,92
>>>>
>>>> 49
>>>>
>>>> A
>>>>
>>>> Santo Antônio do Descoberto
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 49
>>>>
>>>> A
>>>>
>>>> Santo Antônio do Descoberto
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 49
>>>>
>>>> A
>>>>
>>>> Santo Antônio do Descoberto
>>>>
>>>> 9033,36
>>>>
>>>> 4848,67
>>>>
>>>> 13131,13
>>>>
>>>> 4320,57
>>>>
>>>> 8121,07
>>>>
>>>> 4410,8
>>>>
>>>> 8562,85
>>>>
>>>> 6241,59
>>>>
>>>> 8261,5
>>>>
>>>> 5586,92
>>>>
>>>> 64
>>>>
>>>> D
>>>>
>>>> Goiânia
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 64
>>>>
>>>> D
>>>>
>>>> Goiânia
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 64
>>>>
>>>> D
>>>>
>>>> Goiânia
>>>>
>>>> 4091,54
>>>>
>>>> 3076,74
>>>>
>>>> 5482,15
>>>>
>>>> 2243,94
>>>>
>>>> 3380,04
>>>>
>>>> 2033,13
>>>>
>>>> 3003,66
>>>>
>>>> 2250,64
>>>>
>>>> 3601,44
>>>>
>>>> 2550,23
>>>>
>>>> 64
>>>>
>>>> D
>>>>
>>>> Goiânia
>>>>
>>>> 4091,54
>>>>
>>>> 3076,74
>>>>
>>>> 5482,15
>>>>
>>>> 2243,94
>>>>
>>>> 3380,04
>>>>
>>>> 2033,13
>>>>
>>>> 3003,66
>>>>
>>>> 2250,64
>>>>
>>>> 3601,44
>>>>
>>>> 2550,23
>>>>
>>>> 23
>>>>
>>>> E
>>>>
>>>> Anicuns
>>>>
>>>> 6833,28
>>>>
>>>> 3772,8
>>>>
>>>> 8706,81
>>>>
>>>> 2961,72
>>>>
>>>> 5524,23
>>>>
>>>> 3285,14
>>>>
>>>> 6157,68
>>>>
>>>> 4381,01
>>>>
>>>> 5997,78
>>>>
>>>> 4178,95
>>>>
>>>> 23
>>>>
>>>> E
>>>>
>>>> Anicuns
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 23
>>>>
>>>> E
>>>>
>>>> Anicuns
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 23
>>>>
>>>> E
>>>>
>>>> Anicuns
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 0
>>>>
>>>> 23
>>>>
>>>> E
>>>>
>>>> Anicuns
>>>>
>>>> 6833,28
>>>>
>>>> 3772,8
>>>>
>>>> 8706,81
>>>>
>>>> 2961,72
>>>>
>>>> 5524,23
>>>>
>>>> 3285,14
>>>>
>>>> 6157,68
>>>>
>>>> 4381,01
>>>>
>>>> 5997,78
>>>>
>>>> 4178,95
>>>>
>>>>
>>>> Obrigado pela ajuda,
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> 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.
>>>
>>
>>
>>
>> --
>> =========================================
>> Fernando Souza
>> Zootecnista, DSc. Produção e Alimentação Animal
>> Celular: (31)99796-8781 (Vivo)
>> E-mail:nandodesouza em gmail.com <e-mail%3Anandodesouza em gmail.com>
>> Lattes: http://lattes.cnpq.br/6519538815038307
>> Blog: https://producaoanimalcomr.wordpress.com/
>> ==========================================
>>
>
>
>
> _______________________________________________
> 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.
>
-------------- Próxima Parte ----------
Um anexo em HTML foi limpo...
URL: <http://listas.inf.ufpr.br/pipermail/r-br/attachments/20170504/6f48a3b9/attachment-0001.html>
Mais detalhes sobre a lista de discussão R-br