问题
I have a problem changing the Azure Cost Management response into a data frame.
This is what I get from AzureRMR
:
response_example <-
list(id = 'subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.CostManagement/query/00000000-0000-0000-0000-000000000000',
name = '00000000-0000-0000-0000-000000000000',
type = 'Microsoft.CostManagement/query',
location = NULL,
sku = NULL,
eTag = NULL,
properties = list(
nextLink = 'https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.CostManagement/query?api-version=2019-11-01&$skiptoken=000000%3D%3D',
columns = list(
list(name = "UsageDate",
type = "Number"),
list(name = "Currency",
type = "String")
),
rows = list(
list(as.integer(20200401),
"EUR"),
list(as.integer(20200402),
"EUR")
)
))
response_example
#> $id
#> [1] "subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.CostManagement/query/00000000-0000-0000-0000-000000000000"
#>
#> $name
#> [1] "00000000-0000-0000-0000-000000000000"
#>
#> $type
#> [1] "Microsoft.CostManagement/query"
#>
#> $location
#> NULL
#>
#> $sku
#> NULL
#>
#> $eTag
#> NULL
#>
#> $properties
#> $properties$nextLink
#> [1] "https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.CostManagement/query?api-version=2019-11-01&$skiptoken=000000%3D%3D"
#>
#> $properties$columns
#> $properties$columns[[1]]
#> $properties$columns[[1]]$name
#> [1] "UsageDate"
#>
#> $properties$columns[[1]]$type
#> [1] "Number"
#>
#>
#> $properties$columns[[2]]
#> $properties$columns[[2]]$name
#> [1] "Currency"
#>
#> $properties$columns[[2]]$type
#> [1] "String"
#>
#>
#>
#> $properties$rows
#> $properties$rows[[1]]
#> $properties$rows[[1]][[1]]
#> [1] 20200401
#>
#> $properties$rows[[1]][[2]]
#> [1] "EUR"
#>
#>
#> $properties$rows[[2]]
#> $properties$rows[[2]][[1]]
#> [1] 20200402
#>
#> $properties$rows[[2]][[2]]
#> [1] "EUR"
Created on 2020-05-07 by the reprex package (v0.3.0)
I managed to get the column names:
library(tidyverse)
colnames <- map_chr(response_example[["properties"]][["columns"]], 1)
colnames
#> [1] "UsageDate" "Currency"
Created on 2020-05-07 by the reprex package (v0.3.0)
Edit: I found a way to get the result I want:
t1 <- map_dfc(transpose(response_example[["properties"]][["rows"]]) %>% set_names(colnames), as.character)
t1
#> # A tibble: 2 x 2
#> UsageDate Currency
#> <chr> <chr>
#> 1 20200401 EUR
#> 2 20200402 EUR
t2 <- map_dfr(transpose(response_example[["properties"]][["rows"]]) %>% set_names(colnames), as.character)
t2
#> # A tibble: 2 x 2
#> UsageDate Currency
#> <chr> <chr>
#> 1 20200401 EUR
#> 2 20200402 EUR
Created on 2020-05-07 by the reprex package (v0.3.0)
I'm still looking for the cleanest way to build a data frame with proper column names and values in rows. The response is multipage and I need to bind the responses together in a loop.
来源:https://stackoverflow.com/questions/61653928/turning-azure-cost-management-apis-response-into-data-frame