rtibblespread

Spread or gather a Tibble


I get a tibble from an API, API ANBIMA, that put the data in two columns, one with the name of the variables that i want to be the rows and other with the values. I was trying to put the first column in a list, filter for unique values, paste in a spread command, but i can't do the first thing, that was create a list to begin with. When i tried, i'd get a chr list with one observation.

The data have 15 variables, that repeat itself in the first column, varying the values of the second column

EDIT:

Like was suggested, here is the output of dput(head(x, 20)

dput(head(dados, 20))
structure(list(name = c("tipo_titulo", "expressao", "data_vencimento", 
"data_referencia", "codigo_selic", "data_base", "taxa_compra", 
"taxa_venda", "taxa_indicativa", "intervalo_min_d0", "intervalo_max_d0", 
"intervalo_min_d1", "intervalo_max_d1", "pu", "desvio_padrao", 
"tipo_titulo", "expressao", "data_vencimento", "data_referencia", 
"codigo_selic"), value = c("LTN", "Taxa (% a.a.)/252", "2023-01-01", 
"2022-12-06", "100000", "2016-01-20", "13.7107", "13.6952", "13.7027", 
"13.5377", "13.9248", "13.5338", "13.9233", "990.36449", "0.00259307377643", 
"NTN-F", "Taxa (% a.a.)/252", "2023-01-01", "2022-12-06", "950199"
)), row.names = c(NA, 20L), class = c("tbl_df", "tbl", "data.frame"
))

I'm at my desktop from work, so i can't access a host to upload the data, i'll put a picture that show the format of the tibble

enter image description here

The API info to access is in here: https://developers.anbima.com.br/en/visao-geral/autenticacao/#oauth2

To get the tibble, i used the code:

library(httr)
library(jsonlite)
library(tidyverse)
library(stringr)
library(tibble)
library(bizdays)

client_id <- ''
client_secret <- ''

token_anbima <- base64_enc(str_c(client_id, ':', client_secret))

resposta_anbima <- POST("https://api.anbima.com.br/oauth/access-token",
                        add_headers("Authorization" = str_c("Basic ", token_anbima)), 
                        body = list(grant_type = "client_credentials"),
                        encode = 'form'
                        )

resposta_anbima$status

access_token <- str_sub(content(resposta_anbima, "text"), start = 18, end = 29)

data_tpf <- preceding(Sys.Date(), 'Brazil/ANBIMA')

teste <- GET(str_c('https://api-sandbox.anbima.com.br/feed/precos-indices/v1/titulos-publicos/mercado-secundario-TPF?data=',
                   data_tpf),
             add_headers("client_id" = client_id,
                         "access_token" = access_token))

dados <- enframe(unlist(content(teste)))

Solution

  • Using pivot_wider in conjunction with unnest, though not entirely sure if this is what you need.

    library(tidyr)
    
    pivot_wider(dados, names_from=name, values_from=value, values_fn=list) %>% 
      unnest(everything())
    # A tibble: 2 × 15
      tipo_titulo expressao  data_…¹ data_…² codig…³ data_…⁴ taxa_…⁵ taxa_…⁶ taxa_…⁷
      <chr>       <chr>      <chr>   <chr>   <chr>   <chr>   <chr>   <chr>   <chr>  
    1 LTN         Taxa (% a… 2023-0… 2022-1… 100000  2016-0… 13.7107 13.6952 13.7027
    2 NTN-F       Taxa (% a… 2023-0… 2022-1… 950199  2016-0… 13.7107 13.6952 13.7027
    # … with 6 more variables: intervalo_min_d0 <chr>, intervalo_max_d0 <chr>,
    #   intervalo_min_d1 <chr>, intervalo_max_d1 <chr>, pu <chr>,
    #   desvio_padrao <chr>, and abbreviated variable names ¹​data_vencimento,
    #   ²​data_referencia, ³​codigo_selic, ⁴​data_base, ⁵​taxa_compra, ⁶​taxa_venda,
    #   ⁷​taxa_indicativa