colearendt / tidyjson

Tidy your JSON data in R with tidyjson
Other
182 stars 15 forks source link

Create a spread_array function #94

Open jeremystan opened 7 years ago

jeremystan commented 7 years ago

From Hadley:

You definitely also need a spread_array() to complement gather_array(). It's a rarer operation but still useful.

JSON arrays don't have names, so perhaps this would work like:

'[1, 2, 3]' %>% spread_array_int()
# A tbl_json: 1 x 3 tibble with a "JSON" attribute
  `attr(., "JSON")` document.id value.1 value.2 value.3
              <chr>       <int>   <int>   <int>   <int>
1         [1, 2, 3]           1       1       2       3
colearendt commented 7 years ago

Maybe add a selection of index, as well... A common problem is when an array has only one item, or you only care about the first / last item, etc. It is tedious to gather, filter, and then spread the array, not to mention joining back to the original data, when all that is really needed is a spread.