I have a tibble and a list which I would like to write to a json file.
# A tibble: 2 x 12
i n c x
<chr> <chr> <chr> <chr>
1 NYC New York City United States LON,271;BOS,201
2 LON London United Kingdom NYC,270
I would like to replace the 'x' column with a list.
When I try to merge by the 'i' column with the element of the list, a lot of data is duplicated... :/
sample list:
$NYC
d p
1: LON 271
2: BOS 201
$LON
d p
1: NYC 270
I would like to end up with something that looks like this:
[
{
"i": "NYC",
"n": "New York City",
"c": "United States",
"C": "US",
"r": "Northern America",
"F": 66.256,
"L": -166.063,
"b": 94.42,
"s": 0.752,
"q": 4417,
"t": "0,0,0,0,0",
"x": [{
"d": "LON",
"p": 271
},
{
"d": "BOS",
"p": 201
}]
}
...
]
I'm thinking there should be a way to write the json file without merging the list and the tibble, or maybe there is a way to merge them in a ragged way ?
ah. I just had another idea. maybe I can convert my dataframe to a list then use Reduce to combine the lists...
http://www.sharecsv.com/s/2e1dc764430c6fe746d2299f71879c2e/routes-before-split.csv
http://www.sharecsv.com/s/b114e2cc6236bd22b23298035fb7e042/tibble.csv
We may do the following:
The latter two lines are a shortened version of this base R equivalent:
We go over the
x
column, split its elements by;
and,
whenever possible, and then use the fact that the resulting odd elements will correspond do thed
column in the desired outcome, and the even elements to thep
column.Output: