Thanks @Yarnabrina! Now I realize my question was not clear enough. Consider this different output
tibble::tibble(tricky = c('[10,20,30,40]',
'[100,200,300,400]',
'[2,4,6,8]'),
group = c('a', 'a', 'b'))
# A tibble: 3 x 2
tricky group
<chr> <chr>
1 [10,20,30,40] a
2 [100,200,300,400] a
3 [2,4,6,8] b
Now, to achieve what I want to do, I was able to use the following
tibble::tibble(tricky = c('[10,20,30,40]',
'[100,200,300,400]',
'[2,4,6,8]'),
group = c('a', 'a', 'b')) %>%
mutate(mylist = map(tricky, ~jsonlite::parse_json(.x))) %>% unnest(mylist) %>%
mutate(mylist = as.numeric(mylist)) %>% group_by(tricky) %>%
mutate(idx = row_number()) %>% ungroup()
# A tibble: 12 x 4
tricky group mylist idx
<chr> <chr> <dbl> <int>
1 [10,20,30,40] a 10 1
2 [10,20,30,40] a 20 2
3 [10,20,30,40] a 30 3
4 [10,20,30,40] a 40 4
5 [100,200,300,400] a 100 1
6 [100,200,300,400] a 200 2
7 [100,200,300,400] a 300 3
8 [100,200,300,400] a 400 4
9 [2,4,6,8] b 2 1
10 [2,4,6,8] b 4 2
11 [2,4,6,8] b 6 3
12 [2,4,6,8] b 8 4
However, this looks super clunky and non-efficient to me. Also, the as.numeric(mylist) seems odd and importantly, I am not entirely sure that my grouping by tricky will preserve the row-order so that my index is the right one.
You see what I mean? Can we do better here?
Thanks!!!