Rstudio - Update and add new data

Asked

Viewed 91 times

1

Hello,

I’m looking for IBGE data (library("sidrar")),

df <- get_sidra (api = "/t/1846/n1/all/v/all/p/all/c11255/90707/d/v585%200")
#Selecionando as colunas que me interessa
df1 <- df[,c(7,10,13)]

This returns me the historical series of GDP

df1 <- Trimestre (Código)           Setores e subsetores           Valor
        199601                      PIB a preço de mercado         189323
        ...                             ...                         ...
        201903                      PIB a preço de mercado         1842700
        201904                      PIB a preço de mercado         1842740

And saved this file on my computer

read.xlsx("df1.xlsx", sheetName = "PIB", header = TRUE)

When a new data comes out, 202001, IBGE revises 3 previous data (201902, 201903, 201904) and publishes 202001,

then I just run the code to fetch the last 4 data, to avoid having to fetch the complete series

df2 <- get_sidra (api = "/t/1846/n1/all/v/all/p/last%203/c11255/90707/d/v585%200")

After having my df2, I want to update the data for 201902, 201903 and 201904, and add the data for 202001

To add only the new data, I was using :

wb <- loadWorkbook("df1.xlsx")
addDataFrame(df2,getSheets(wb)$df2,  col.names = FALSE, row.names = FALSE, startRow = nrow(read.xlsx("df1.xlsx", sheetName = "PIB"))+2)
saveWorkbook(wb,"df1.xlsx")

But this function doesn’t help me update my previous information. Could someone tell me how I can do this?

  • 4

    Please read How to create a minimum playable example in R and create a [mcve]

  • Do you need to use XLS? If you are dealing with large databases that need regular updating, it is more efficient to use a database format. Recommend SQL, which can be accessed by Excel and has R-friendly packages (such as dbplyr). If you really need to use XLS, update your question with a minimal playable example and indicate the package you are using to export the data.

  • @Carloseduardolagosta It would be XLS same, thanks for the help. I am new here and I ended up asking the question in the wrong way, I will update the question according to what Daniel Takeshi sent. Thanks for the help Thanks

  • I’m not sure I understand the question correctly. But it seems you want to join files from different months and cannot use the files that were previously downloaded as they may have been changed. Assuming that these data are downloaded separately on a monthly scale and contain the same type of information, a rbind?

No answers

Browser other questions tagged

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