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

Paulo Nogueira Starzynski paulons em gmail.com
Sábado Janeiro 30 13:17:05 BRST 2016


Alexandre,
no teste que fiz aqui, o problema é na leitura da tabela. A função getURL
não está retornando conteúdo que a função readHTMLTable possa extrair.

> URL="https://www.dropbox.com/s/foeh9oqbiqlawvg/TALHOES_AGENTE2.htm?dl=1"> page <- RCurl::getURL(URL)> page[1] "<html>\r\n  <head><title>Found</title></head>\r\n  <body>\r\n    <h1>Found</h1>\r\n    <p>The resource was found at <a href=\"https://dl.dropboxusercontent.com/content_link/tkauy9obP7pgxKYpb9j5ZR2LCSUTe2iJpaULTWEN4RzdfR6AtirbNWGtt8q9Ncex/file?dl=1\">https://dl.dropboxusercontent.com/content_link/tkauy9obP7pgxKYpb9j5ZR2LCSUTe2iJpaULTWEN4RzdfR6AtirbNWGtt8q9Ncex/file?dl=1</a>;\r\nyou should be redirected automatically.\r\n\r\n<!--  --></p>\r\n    <hr noshade>\r\n    <div align=\"right\">WSGI Server</div>\r\n  </body>\r\n</html>\r\n"


Abraços,
Paulo

Em 30 de janeiro de 2016 12:16, ASANTOS <alexandresantosbr em yahoo.com.br>
escreveu:

> Bom dia Éder,
>
>
>        Tentei criar uma função com os comandos de leitura da tabela em
> HTML e também não funfou, posso pedir novamente sua ajuda, Obrigado,
>
>
>
> ##  Função de leitura da tabela
> readFE<- function (x, URL = ""){
>
> page <- getURL(URL)
> classes <- c(rep("factor", 6), rep("numeric", 4), "factor")
> tableFE <- readHTMLTable(page, head=T, colClasses=classes)[[1]]
>      x<-NULL
>      results <- x
>      results <- x
>      results<-results[!apply(results,1,function(x){any(x=="(NULL)")}),]
>      results<-subset(results, !(latitude == "0.00000000" | longitude ==
> "0.00000000"))
>      results
> }
> #--#
>
>
>
>
>
> ## Tentativa de leitura da tabela
> tableFE<-readFE(URL=
> "https://www.dropbox.com/s/foeh9oqbiqlawvg/TALHOES_AGENTE2.htm?dl=1"
> <https://www.dropbox.com/s/foeh9oqbiqlawvg/TALHOES_AGENTE2.htm?dl=1>)
> head(tableFE)
> #---------------
>
>
>
>
> --
> ======================================================================
> 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 29/01/2016 08:41, Éder Comunello escreveu:
>
> Alexandre, bom dia!
>
> Não havia atentado para o problema na importação das tabelas, sendo
> necessário definir as classes. Além disso, na função você deve se referir a
> "db" antes que "tableFE" e  "x" é definido internamente por lapply().
>
>
> ### <code r>
> require(RCurl); require(XML)
> url0 <- "https://www.dropbox.com/s/znmr0lwda2y6fc5/BD_teste2.html?dl=1"
> url1 <- gsub("(^.*)(/s/.*)(\\?.*$)", " <https://dl.dropboxusercontent.com>
> https://dl.dropboxusercontent.com\\2", url0); url1
> # [1] "https://dl.dropboxusercontent.com/s/znmr0lwda2y6fc5/BD_teste2.html"
>
> page <- getURL(url1)
> tableFE <- readHTMLTable(page, head=T)[[1]]
> str(tableFE) ### todas variáveis estão como fatores - corrigir!
>
> classes <- c(rep("factor", 6), rep("numeric", 4), "factor")
> tableFE <- readHTMLTable(page, head=T, colClasses=classes)[[1]]
> str(tableFE) ### OK!
> head(tableFE)
>
> ##Agregando os resultados
> aggPestFE <- function(db=tableFE, key="descricao"){
> lista  <- split(db, db[key])
> result <- lapply(lista, function(x) aggregate(x[,7:8],
> by=list(x[,3],x[,2],x[,5]), mean))
> return(result)
> }
>
> ### Devido aos "defaults", obtém mesmos resultados nas três formas que
> seguem:
> aggPestFE()[5]
> aggPestFE(tableFE)[5]
> aggPestFE(tableFE, "descricao")[5]
> # $`Lagartas Desfolhadoras`
> #   Group.1           Group.2 Group.3 formiga_area qtd_destruido
> # 1      GN             Chale      26            0          62.5
> # 2      RD Corrego da Coruja      26            0          50.0
> # 3      GN      Aeroporto II      28            0          75.0
>
> ### </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]
>
>
>
>
>
> _______________________________________________
> 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/20160130/f43a5d86/attachment.html>


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