[R-br] Criar função para tirar a média entre grupos sob condições dadas [RESOLVIDO]

ASANTOS alexandresantosbr em yahoo.com.br
Quinta Janeiro 21 23:16:24 BRST 2016


Muito obrigado Éder e Manuel, faltou o pacote httr,

Abraços,

-- 
======================================================================
Alexandre dos Santos
Proteção Florestal
IFMT - Instituto Federal de Educação, Ciência e Tecnologia de Mato Grosso
Campus Cáceres
Caixa Postal 244
Avenida dos Ramires, s/n
Bairro: Distrito Industrial
Cáceres - MT                      CEP: 78.200-000
Fone: (+55) 65 8132-8112 (TIM)   (+55) 65 9686-6970 (VIVO)
e-mails:alexandresantosbr em yahoo.com.br
         alexandre.santos em cas.ifmt.edu.br
Lattes: http://lattes.cnpq.br/1360403201088680
OrcID: orcid.org/0000-0001-8232-6722
Researchgate: https://www.researchgate.net/profile/Alexandre_Santos10
LinkedIn: https://br.linkedin.com/in/alexandre-dos-santos-87961635
======================================================================

Em 21/01/2016 21:48, Éder Comunello escreveu:
> Alexandre,
>
> Tente a seguinte linha:
>
> lapply(lista, function(x) aggregate(x[,7:8], by=list(x[,2], x[,5]), 
> mean))
>
>> ================================================
> Éder Comunello
> PhD Student in Agricultural Systems Engineering (USP/Esalq)
> Brazilian Agricultural Research Corporation (Embrapa)
> Dourados, MS, Brazil [22 16.5'S, 54 49.0'W]
>
>
>
>
> Em 21 de janeiro de 2016 17:55, Manoel Galdino <mcz.fea em gmail.com 
> <mailto:mcz.fea em gmail.com>> escreveu:
>
>     Nem consigo executar sua função de begar o banco, diz q não existe GET
>
>     cheguei a carrega o pacote XML, mas tbm não funfou
>
>     2016-01-21 18:28 GMT-02:00 ASANTOS <alexandresantosbr em yahoo.com.br
>     <mailto:alexandresantosbr em yahoo.com.br>>:
>
>         Éder,
>
>            A sua solução não funcionou com meu exemplo real e não
>         consigo achar o problema, sendo que:
>
>         ##  Função de leitura do banco de dados
>         readFE<- function (x, URL = ""){
>         FILE <- GET(url=URL)
>              tables <- getNodeSet(htmlParse(FILE), "//table")
>              FE_tab <- readHTMLTable(tables[[1]],
>                                 header =
>         c("empresa","desc_projeto","desc_regiao",
>         "cadastrador_por","cod_talhao","descricao",
>         "formiga_area","qtd_destruido","latitude",
>         "longitude","data_cadastro"),
>                                 colClasses =
>         c("character","character","character",
>         "character","numeric","character",
>         "numeric","numeric","numeric",
>         "numeric","character"),
>                                 trim = TRUE, stringsAsFactors = FALSE
>         )
>              x<-NULL
>              results <- x
>              x<-FE_tab[-(1),]
>              results <- x
>         results<-results[!apply(results,1,function(x){any(x=="(NULL)")}),]
>              results
>         }
>         #--#
>         tableFE<-readFE(URL="https://www.dropbox.com/s/znmr0lwda2y6fc5/BD_teste2.html?dl=1"
>         <https://www.dropbox.com/s/znmr0lwda2y6fc5/BD_teste2.html?dl=1>)
>         tableFE<-tableFE[1:163,1:11]
>         head(tableFE)
>
>         ##Agregando os resultados
>         lista <- split(tableFE, tableFE$descricao)
>         lapply(lista, function(x) aggregate(x[,7:8], by=list(x[,2]),
>         mean))  ## Não funciona
>
>
>         mas se eu faço item por item funciona e não sei porque, sendo:
>
>         aggregate(lista$`Psilideo-de-Concha`[,7:8],
>         by=list(lista$`Psilideo-de-Concha`[,2],lista$`Psilideo-de-Concha`[,5]),
>         mean)
>
>         Poderia me dar mais um help?
>
>         Obrigado,
>
>         Abraços
>
>         -- 
>         ======================================================================
>         Alexandre dos Santos
>         Proteção Florestal
>         IFMT - Instituto Federal de Educação, Ciência e Tecnologia de Mato Grosso
>         Campus Cáceres
>         Caixa Postal 244
>         Avenida dos Ramires, s/n
>         Bairro: Distrito Industrial
>         Cáceres - MT                      CEP: 78.200-000
>         Fone:(+55) 65 8132-8112 <tel:%28%2B55%29%2065%208132-8112>  (TIM)(+55) 65 9686-6970 <tel:%28%2B55%29%2065%209686-6970>  (VIVO)
>         e-mails:alexandresantosbr em yahoo.com.br
>         <mailto:e-mails:alexandresantosbr em yahoo.com.br>  
>                  alexandre.santos em cas.ifmt.edu.br
>         <mailto:alexandre.santos em cas.ifmt.edu.br>  
>         Lattes:http://lattes.cnpq.br/1360403201088680
>         OrcID:orcid.org/0000-0001-8232-6722
>         <http://orcid.org/0000-0001-8232-6722>  
>         Researchgate:https://www.researchgate.net/profile/Alexandre_Santos10                        
>         LinkedIn:https://br.linkedin.com/in/alexandre-dos-santos-87961635
>         ======================================================================
>
>         Em 21/01/2016 13:37, Éder Comunello escreveu:
>>         Senhores, boa tarde!
>>
>>         Também não entendi direito. Mas talvez a ideia abaixo possa
>>         fazer algum sentido...
>>
>>         ### <code r>
>>         tableFE <- structure(list(Bichos = structure(c(2L, 1L, 3L,
>>         1L, 3L, 2L, 3L,
>>         2L, 1L, 3L, 1L, 3L, 2L, 3L, 3L), .Label = c("Barata",
>>         "Besouros",
>>         "Formiga"), class = "factor"), Talhao = c(73, 15, 74, 75, 15,
>>         15, 15, 73, 15, 15, 73, 15, 73, 74, 74), Projeto =
>>         structure(c(1L,
>>         3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L),
>>         .Label = c("Abre Campo",
>>         "Vitoria", "Volta Redonda"), class = "factor"), Injuria = c(25,
>>         100, 0, 25, 0, 100, 0, 50, 25, 0, 25, 0, 25, 0, 0), Area = c(0,
>>         0, 12.5, 0, 7.5, 0, 1.5, 0, 0, 23.8, 0, 5.3, 0, 2, 11.3)),
>>         .Names = c("Bichos",
>>         "Talhao", "Projeto", "Injuria", "Area"), row.names = c(NA, -15L
>>         ), class = "data.frame")
>>
>>         lista <- split(tableFE, tableFE$Bichos); lista
>>
>>         lapply(lista, function(x) aggregate(x[,4:5],
>>         by=list(x$Projeto), mean))
>>         # $Barata
>>         #     Group.1 Injuria Area
>>         # 1  Abre Campo    25.0    0
>>         # 2     Vitoria    25.0    0
>>         # 3 Volta Redonda    62.5    0
>>         #
>>         # $Besouros
>>         #     Group.1 Injuria Area
>>         # 1  Abre Campo      25    0
>>         # 2     Vitoria     100    0
>>         # 3 Volta Redonda      50    0
>>         #
>>         # $Formiga
>>         #     Group.1 Injuria  Area
>>         # 1  Abre Campo       0 12.65
>>         # 2     Vitoria       0  9.70
>>         # 3 Volta Redonda       0  4.75
>>         ### </code>
>>
>>>>         ================================================
>>         Éder Comunello
>>         PhD Student in Agricultural Systems Engineering (USP/Esalq)
>>         Brazilian Agricultural Research Corporation (Embrapa)
>>         Dourados, MS, Brazil [22 16.5'S, 54 49.0'W]
>>
>>
>>
>>
>>         Em 21 de janeiro de 2016 11:41, Manoel Galdino
>>         <mcz.fea em gmail.com <mailto:mcz.fea em gmail.com>> escreveu:
>>
>>             Eu não entendi o que você quer fazer. No seu exemplo, o
>>             primeiro elemento é besouro, mas você retornou barata.
>>
>>             Os valores de talhao e area nnao parecem bater com os
>>             dados do seu exemplo. Como para Formiga o talhão de volta
>>             redonda seria 73, se na base original os valores são 74 e 15?
>>
>>             Abc
>>             Manoel
>>
>>             > tableFE
>>
>>                  Bichos Talhao Projeto Injuria Area
>>               Besouros     73    Abre Campo      25  0.0
>>                 Barata     15 Volta Redonda     100  0.0
>>               Formiga     74 Vitoria       0 12.5
>>                Barata     75    Abre Campo      25  0.0
>>               Formiga     15 Volta Redonda       0  7.5
>>               Besouros     15 Vitoria     100  0.0
>>                Formiga     15    Abre Campo       0  1.5
>>               Besouros     73 Volta Redonda      50  0.0
>>                Barata     15 Vitoria      25  0.0
>>               Formiga     15    Abre Campo       0 23.8
>>                Barata     73 Volta Redonda      25  0.0
>>               Formiga     15 Vitoria       0  5.3
>>              Besouros     73    Abre Campo      25  0.0
>>               Formiga     74 Volta Redonda       0  2.0
>>               Formiga     74 Vitoria       0 11.3
>>
>>             Mas seus resultados não têm a média
>>
>>             2016-01-21 11:07 GMT-02:00 ASANTOS
>>             <alexandresantosbr em yahoo.com.br
>>             <mailto:alexandresantosbr em yahoo.com.br>>:
>>
>>                 Caros listeiros,
>>
>>
>>                    Estou tentando criar uma função aggPestFE que para
>>                 uma primeira variável se for "Formiga", vai tirar a
>>                 media da coluna 5, mas se for "Besouros" ou "Barata"
>>                 vai tira a média da coluna 4. Sendo que quero o
>>                 resultado dado em forma de list, ficando o meu resultado:
>>
>>                 [[1]]
>>                 [1] "Barata"
>>
>>                 [[1]][[3]]
>>                         Projeto Talhao Injuria(%)
>>                 1    Abre Campo  15        25
>>                 2 Volta Redonda  73        0
>>                 3       Vitoria  74        50
>>
>>
>>                 [[2]]
>>                 [1] "Besouros"
>>
>>                 [[1]][[3]]
>>                         Projeto Talhao Injuria(%)
>>                 1    Abre Campo  15        0
>>                 2 Volta Redonda  73        25
>>                 3       Vitoria  74        50
>>
>>                 [[2]]
>>                 [1] "Formiga"
>>
>>                 [[1]][[3]]
>>                         Projeto Talhao   Area  (m2)
>>                 1    Abre Campo  15        12.5
>>                 2 Volta Redonda  73        1.5
>>                 3       Vitoria  74        23.8
>>
>>
>>                            Para isso estou tentando:
>>
>>
>>                 ##Dados artificiais
>>
>>                 Bichos<-c("Besouros","Barata","Formiga","Barata","Formiga","Besouros","Formiga",
>>                 "Besouros","Barata","Formiga","Barata","Formiga","Besouros","Formiga","Formiga")
>>
>>                 Talhao<-c(73,15,74,75,15,15,15,73,15,15,73,15,73,74,74)
>>
>>
>>                 Projeto<-c("Abre Campo", "Volta
>>                 Redonda","Vitoria","Abre Campo", "Volta Redonda",
>>                 "Vitoria","Abre Campo", "Volta
>>                 Redonda","Vitoria","Abre Campo", "Volta Redonda",
>>                 "Vitoria","Abre Campo", "Volta Redonda","Vitoria")
>>
>>                 Injuria<-c(25,100,0,25,0,100,0,50,25,0,25,0,25,0,0)
>>
>>
>>
>>                 Area<-c(0,0,12.5,0,7.5,0,1.5,0,0,23.8,0,5.3,0,2.0,11.3)
>>
>>
>>                 tableFE<-data.frame(Bichos, Talhao, Projeto, Injuria,
>>                 Area) ## Banco de dados criado
>>
>>
>>                 ##Função
>>
>>                 aggPestFE<-function(x, db=tableFE){
>>
>>
>>                 mylist <- list()
>>
>>
>>
>>
>>                      if (tableFE[,1] != "Formigas") {
>>
>>                         for (i in length(tableFE[,1])){
>>
>>                 GP_FE2<-tableFE[tableFE[,1]==tableFE[i],]
>>                         aggdata <-aggregate(GP_FE2[,4],
>>                 list(GP_FE2[,3],GP_FE2[,2]), mean)
>>                 colnames(aggdata)<-c("Regional","Projeto","Talhão","Injúria
>>                 média (%)")
>>                         tmp <- list(aggdata)
>>                 mylist[NPRAGS[i]] <- tmp
>>                 result<-mylist
>>                 return(result)
>>                         }
>>
>>
>>                      else if (tableFE[,1] == "Formigas") {
>>
>>                 GP_FE2<-[tableFE[,1]
>>                         aggdata <-aggregate(GP_FE2[,5],
>>                 list(GP_FE2[,3],GP_FE2[,2]), mean)
>>                 colnames(aggdata)<-c("Regional","Projeto","Talhão","Injúria
>>                 média (%)")
>>                         tmp <- list(aggdata)
>>                 mylist[NPRAGS[i]] <- tmp
>>                 result<-mylist
>>                 return(result)
>>                         }
>>                         }
>>
>>                 RES<-list(result,aggdata3)
>>                 result<-RES
>>                 return(result)
>>                 }
>>
>>
>>                 #Teste
>>                 aggPestFE(tableFE)
>>                 #
>>
>>                    Sem sucesso, alguém poderia me ajudar?
>>
>>                 Obrigado e abraços,
>>
>>
>>
>>
>>
>>
>>                 -- 
>>                 ======================================================================
>>                 Alexandre dos Santos
>>                 Proteção Florestal
>>                 IFMT - Instituto Federal de Educação, Ciência e
>>                 Tecnologia de Mato Grosso
>>                 Campus Cáceres
>>                 Caixa Postal 244
>>                 Avenida dos Ramires, s/n
>>                 Bairro: Distrito Industrial
>>                 Cáceres - MT               CEP: 78.200-000
>>                 Fone: (+55) 65 8132-8112
>>                 <tel:%28%2B55%29%2065%208132-8112> (TIM) (+55) 65
>>                 9686-6970 <tel:%28%2B55%29%2065%209686-6970> (VIVO)
>>                 e-mails:alexandresantosbr em yahoo.com.br
>>                 <mailto:e-mails%3Aalexandresantosbr em yahoo.com.br>
>>                 alexandre.santos em cas.ifmt.edu.br
>>                 <mailto:alexandre.santos em cas.ifmt.edu.br>
>>                 Lattes: http://lattes.cnpq.br/1360403201088680
>>                 OrcID: orcid.org/0000-0001-8232-6722
>>                 <http://orcid.org/0000-0001-8232-6722>
>>                 Researchgate:
>>                 https://www.researchgate.net/profile/Alexandre_Santos10
>>                 LinkedIn:
>>                 https://br.linkedin.com/in/alexandre-dos-santos-87961635
>>                 ======================================================================
>>
>>                 _______________________________________________
>>                 R-br mailing list
>>                 R-br em listas.c3sl.ufpr.br
>>                 <mailto: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 fornea cdigo
>>                 mnimo reproduzvel.
>>
>>
>>
>>
>>             -- 
>>             Manoel Galdino
>>             https://sites.google.com/site/galdinomcz/
>>
>>             _______________________________________________
>>             R-br mailing list
>>             R-br em listas.c3sl.ufpr.br <mailto: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
>>         <mailto: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 <mailto: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.
>
>
>
>
>     -- 
>     Manoel Galdino
>     https://sites.google.com/site/galdinomcz/
>
>     _______________________________________________
>     R-br mailing list
>     R-br em listas.c3sl.ufpr.br <mailto: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.

-------------- Próxima Parte ----------
Um anexo em HTML foi limpo...
URL: <http://listas.inf.ufpr.br/pipermail/r-br/attachments/20160121/a07f1e53/attachment-0001.html>


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