I am struggling with a simple question.
Consider this example
> tibble(tricky = '[10,20,30,40]') %>%
+ mutate(mylist = map(tricky, ~jsonlite::parse_json(.x)))
# A tibble: 1 x 2
tricky mylist
<chr> <list>
1 [10,20,30,40] <list [4]>
As you can see, I am able to convert the python
list [10,20,30,40]
into a list-column
.
Here the idea is that element 10
corresponds to index position 1
, 20
corrresponds to index position 2
, etc. I other words, the order in the original tricky
character is extremely important.
I am trying to unlist
that variable so that the tibble contains both the element and its index position:
# A tibble: 4 x 2
idx value
<dbl> <dbl>
1 1 10
2 2 20
3 3 30
4 4 40
How can I do that? I tried with some variations like
tibble(tricky = '[10,20,30,40]') %>%
mutate(mylist = map(tricky, ~jsonlite::parse_json(.x))) %>%
mutate(test = map(mylist, ~unlist(.x, use.names = TRUE)))
with no success. Thanks!