Import xls file to R

Asked

Viewed 535 times

1

The data can be obtained by Link <- https://github.com/marcosvafg/salario_juizes

But precisely the file name is Salarios_juizes_tjsp_122017. The file is protected, so I can’t change it. I wanted to

Importing the xls file

only the spreadsheet Paycheck

discarding the header

discarding the first 20 lines and importing only the selected columns of [1, 2, 3, 4, 8, 14]. In python I can do it, but in R I have difficulty importing. Somebody help me please.

1 answer

3

Apparently, you will need to use the function readxl::read_excel to do this job. For this, you will need to download the spreadsheet first and read it in your working directory (as it is not possible to pass the URL in this function). The function openxlsx::read.xlsx() allows you to enter with the URL, but does not read the format .xls

So it follows code:

setwd("caminho/que/o/arquivo/foi/salvo/na/sua/maquina")
df <- read_excel("Salarios_Juizes_TJSP_122017.xls", 
                 skip = 20,    # Pula as 20 primeiras linhas
                 sheet = 1,    # Tu informa qual é a posição da planilha, nesse caso, coincidiu ser a primeira
                 col_names = F # Informa que o dado não tem cabeçalho
                 )[, c(1:4, 8, 14)] #Não é possível filtrar dentro da função, então é necessário fazer a parte
head(df)
# A tibble: 6 x 6
  X__1      X__2            X__3                X__4                X__8  X__14
  <chr>     <chr>           <chr>               <chr>              <dbl>  <dbl>
1 ***.***.… ABEL APPARECID… Juiz de Direito de… FORUM DA COMARCA…     0   2009.
2 ***.***.… ABELARDO DE AZ… Juiz de Direito de… 2ª VARA CRIMINAL… 12429.  9871.
3 ***.***.… ABEN-ATHAR DE … Desembargador       TRIBUNAL DE JUST… 52520. 15594.
4 ***.***.… ACAUA MULLER F… Juiz de Direito de… 2ª VARA DA COMAR…     0  10927.
5 ***.***.… ACHILE MARIO A… Desembargador       TRIBUNAL DE JUST…     0   7847.
6 ***.***.… ACHILES VICENT… Juiz de Direito de… FORUM DA COMARCA…     0  11834.

I hope someone knows, or a better option arises..

  • 1

    Just remembering that function file.choose() returns the file path (just search the file, click on it and insert the path in the function setwd).

  • 1

    Cool! I use very little, and always forget...

  • Ball show. Thank you.

  • For nothing Fidel!! If the answer was valid, consider accepting it by clicking on the "v" just below the score..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.