How to write a json array from R that has a lat and long sequence?

How to write a json array from R that has a lat and long sequence?

I would like to write:

[[[1,2],[3,4],[5,6]]] 

the best i can do is:

 toJSON(matrix(1:6, ncol = 2, byrow = T)) #"[ [ 1, 2 ],\n[ 3, 4 ],\n[ 5, 6 ] ]" 

How can I wrap a thing in another array (json view)? This is important to me, so I can write files in geojson format as a LineString.

+5
source share
2 answers

I usually use fromJSON to get the target:

 ll <- fromJSON('[[[1,2],[3,4],[5,6]]]') str(ll) List of 1 $ :List of 3 ..$ : num [1:2] 1 2 ..$ : num [1:2] 3 4 ..$ : num [1:2] 5 6 

So, we must create a list of unnamed lists, each of which contains 2 elements:

  xx <- list(setNames(split(1:6,rep(1:3,each=2)),NULL)) identical(toJSON(xx),'[[[1,2],[3,4],[5,6]]]') [1] TRUE 
+5
source

If you have matrix

  m1 <- matrix(1:6, ncol=2, byrow=T) 

may I help:

  library(rjson) paste0("[",toJSON(setNames(split(m1, row(m1)),NULL)),"]") #[1] "[[[1,2],[3,4],[5,6]]]" 
0
source

Source: https://habr.com/ru/post/1204303/


All Articles