I'm processing queries from a mongo database and was expecting to be able to flatten the results out as shown How am I thinking about this incorrectly?
library(tidyverse)
mongo_return <-
structure(list(
chamber = "House", party_code = 200L, bioname = "ABRAHAM, Ralph",
twitter = "RepAbraham", nominate = structure(list(
dim2 = 0.276,
log_likelihood = -87.3178688959091,
dim1 = 0.509,
conditional = 1L,
number_of_votes = 950L,
geo_mean_probability = 0.912184007904348,
number_of_errors = 27L,
total_number_of_votes = 2105L
),
row.names = c(NA, -1L), class = "data.frame"
)
), row.names = c(NA, -1L), class = "data.frame")
glimpse(mongo_return)
#> Observations: 1
#> Variables: 5
#> $ chamber <chr> "House"
#> $ party_code <int> 200
#> $ bioname <chr> "ABRAHAM, Ralph"
#> $ twitter <chr> "RepAbraham"
#> $ nominate <df[,8]> <data.frame[1 x 8]>
# what I want--this works, but feels wrong
bind_cols(
mongo_return %>% select(-nominate),
mongo_return %>% pull(nominate)
) %>% glimpse()
#> Observations: 1
#> Variables: 12
#> $ chamber <chr> "House"
#> $ party_code <int> 200
#> $ bioname <chr> "ABRAHAM, Ralph"
#> $ twitter <chr> "RepAbraham"
#> $ dim2 <dbl> 0.276
#> $ log_likelihood <dbl> -87.31787
#> $ dim1 <dbl> 0.509
#> $ conditional <int> 1
#> $ number_of_votes <int> 950
#> $ geo_mean_probability <dbl> 0.912184
#> $ number_of_errors <int> 27
#> $ total_number_of_votes <int> 2105
# hoped this would work
mongo_return %>% unnest_wider(nominate)
#> New names:
#> * `` -> ...1
#> New names:
#> * `` -> ...1
#> New names:
#> * `` -> ...1
#> New names:
#> * `` -> ...1
#> New names:
#> * `` -> ...1
#> New names:
#> * `` -> ...1
#> New names:
#> * `` -> ...1
#> New names:
#> * `` -> ...1
#> Error in rep(vec_seq_along(data), n): invalid 'times' argument
# can I pluck?
mongo_return %>% hoist(nominate,
dim1 = "dim1"
)
#> Error in `[[<-.data.frame`(`*tmp*`, .col, value = list(dim2 = 0.276, log_likelihood = -87.3178688959091, : replacement has 8 rows, data has 1
Created on 2019-10-07 by the reprex package (v0.3.0)